I am trying to figure out how to use Regular Expressions to match a word in a sentence a maximum number of times.
Say I have this sentence: "This test sentence is for a test that I have to take, hope I ace this test"
Now say I want to search for the word "test" in this sentence and return a maximum of 2 matches (so I'd hope it just picks up the first and second match and gives up).
Here's what I've tried:
/\btest\b/g
- works great if I want all 3 occurrences of "test"/(\btest\b){0,2}/g
- not sure why this doesn't work, I have grouped the word-boundary plus literal search term expression and have asked it to return 0 to 2.
Thanks in advance for any hints.