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 7783093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:44:31+00:00 2026-06-01T19:44:31+00:00

/\b(keyword|whatever)\b/gi How can I modify the above javascript regex to match only the first

  • 0
/\b(keyword|whatever)\b/gi

How can I modify the above javascript regex to match only the first occurance of each word (I believe this is called non-greedy)?

First occurance of “keyword” and first occurance of “whatever” and I may put more more words in there.

  • 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-06-01T19:44:33+00:00Added an answer on June 1, 2026 at 7:44 pm

    What you’re doing is simply unachievable with a singular regular expression. Instead you will have to store every word you wish to find in an array, loop through them all searching for an answer, and then for any matches, store the result in an array.

    Example:

    var words = ["keyword","whatever"];
    var text = "Whatever, keywords are like so, whatever... Unrelated, I now know " +
               "what it's like to be a tweenage girl. Go Edward.";
    var matches = []; // An empty array to store results in.
    /* When you search the text you need to convert it to lower case to make it
       searchable.
     * We'll be using the built in method 'String.indexOf(needle)' to match 
       the strings as it avoids the need to escape the input for regular expression
       metacharacters. */
    
    //Text converted to lower case to allow case insensitive searchable.
    var lowerCaseText = text.toLowerCase();
    for (var i=0;i<words.length;i++) { //Loop through the `words` array
        //indexOf returns -1 if no match is found
        if (lowerCaseText.indexOf(words[i]) != -1) 
            matches.push(words[i]);    //Add to the `matches` array
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

keyword = house SELECT * FROM products WHERE description LIKE '%house%' This query also
Is keyword metadata currently exposed in the model DD4T creates? I can see a
i get a Keyword not supported: '192.168.1.1;initial catalog'. error when trying to do this
The keyword break in Java can be used for breaking out of a loop
I'm doing a dive into JavaScript, and I am struggling to understand 'this' references
The javascript for keyword will iterate over all properties of an object. If the
This is my first time using StackOverflow myself. I have found many answers to
N.B This question has been significantly edited before the first answer was given. Hi,
The bug is the inputTextarea I have below. Only the last row can record
Possible Duplicate: When do you use the “this” keyword? If I have the following

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.