Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1001051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:36:31+00:00 2026-05-16T07:36:31+00:00

In Javascript i want to be able to match strings that begin with a

  • 0

In Javascript i want to be able to match strings that begin with a certain phrase. However, I want it to be able to match the start of any word in the phrase, not just the beginning of the phrase.

For example:

Phrase: “This is the best”

Need to Match: “th”

Result: Matches Th and th

EDIT: \b works great however it proposes another issue:

It will also match characters after foreign ones. For example if my string is “Männ”, and i search for “n”, it will match the n after Mä…Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:36:31+00:00Added an answer on May 16, 2026 at 7:36 am
    "This is the best moth".match(/\bth/gi);
    

    or with a variable for your string

    var string = "This is the best moth";
    alert(string.match(/\bth/gi));
    

    \b in a regex is a word boundary so \bth will only match a th that at the beginning of a word.

    gi is for a global match (look for all occurrences) and case insensitive

    (I threw moth in there to as a reminder to check that it is not matched)

    jsFiddle example


    Edit:

    So, the above only returns the part that you match (th). If you want to return the entire words, you have to match the entire word.

    This is where things get tricky fast. First with no HTML entity letter:

    string.match(/\bth[^\b]*?\b/gi);
    

    Example

    To match the entire word go from the word boundary \b grab the th followed by non word boundaries [^\b] until you get to another word boundary \b. The * means you want to look for 0 or more of the previous (non word boundaries) the ? mark means that this is a lazy match. In other words it doesn’t expand to as big as would be possible, but stops at the first opportunity.

    If you have HTML entity characters like ä (ä) things get complicated really fast, and you have to use whitespace or whitespace and a set of defined characters that may be at word boundaries.

    string.match(/\sth[^\s]*|^th[^\s]*/gi);
    

    Example with HTML entities.

    Since we’re not using word boundaries, we have to take care of the beginning of the string separately (|^).

    The above will capture the white space at the beginning of words. Using \b will not capture white space, since \b has no width.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to write javascript that executes on a page that
Summary I want to be able to call a JavaScript function from a Flex
I have a html + css + javascript application. I want to be able
I want my JavaScript that's referenced through <script> tags, my css, and various graphic
I'm rewriting a JavaScript project, and I want to be able to use object
I want to be able to add a specified time to a javascript clock
Background I want to be able to parse Javascript source in a Delphi Application.
I want to be able to move objects on my web page with javascript
I want to be able to resize this table background image with javascript i
I want to be able to close an alert box automatically using Javascript after

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.