/\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.
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:
Remove the g modifier from your regex. Then it will find only one match.
Remove
g
flag from your regex:What you're talking about can't be done with a JavaScript regex. It might be possible with advanced regex features like .NET's unrestricted lookbehind, but JavaScript's feature set is extremely limited. And even in .NET, it would probably be simplest to create a separate regex for each word and apply them one by one; in JavaScript it's your only option.
Greediness only applies to regexes that employ quantifiers, like
/START.*END/
. The.
means "any character" and the*
means "zero or more". After theSTART
is located, the.*
greedily consumes the rest of the text. Then it starts backtracking, "giving back" one character at a time until the next part of the regex,END
succeeds in matching.We call this regex "greedy" because it matches everything from the first occurrence of
START
to the last occurrence ofEND
.If there may be more than one "START"-to-"END" sequence, and you want to match just the first one, you can append a
?
to the*
to make it non-greedy:/START.*?END/
. Now, each time the.
tries to consume the next character, it first checks to see if it could matchEND
at that spot instead. Thus it matches from the firstSTART
to the firstEND
after that. And if you want to match all the "START"-to-"END" sequences individually, you add the 'g' modifier:/START.*?END/g
.It's a bit more complicated than that, of course. For example, what if these sequences can be nested, as in
START…START…END…END
? If I've gotten a little carried away with this answer, it's because understanding greediness is the first important step to mastering regexes. :-/