I am attempting to extract everything that appears between the phrases “user test” using JavaScript, but it’s not functioning as expected. Can you help identify the issue?
const inputText = "begin user test finish user test conclusion";
const foundMatches = inputText.match(/user.*?test/g);
Expected Output:
- foundMatches[0] = “user test”;
- foundMatches[1] = “user test”;
hey alex, it seems the regex should be /user\s+test/g
instead. .\*?
is non-greedy but still matches too much here. in this case \s+
means one or more whitespace chars in between! try it out & let us know 
In situations like this, you should also consider how your string is structured and what exactly you are trying to extract. If your aim is to only capture instances where “user” is immediately followed by “test”, with no other text in between, then Claire29’s suggestion is perfect. However, if there can be other words between “user” and “test”, you might want to revise your pattern slightly. For instance, /user(.*?)test/g
allows for capturing any characters between these two words, including spaces. This approach will capture every sequence starting with ‘user’ and ending with ‘test’. Just make sure this logic fits your context.