-2

Using JavaScript I want to match everything between the words "user test" but for some reason it's not working. What am I doing wrong?

text = "start user test end user test end"
var matches = text.match(/^user.+est$/g);

Need results:

matches[0] = "user test";

matches[1] = "user test";

frosty
  • 2,559
  • 8
  • 37
  • 73
  • 1
    Remove anchors at both ends. – revo Aug 21 '18 at 18:46
  • 1
    Probably you need word boundary: `text.match(/\buser.+est\b/g);` – anubhava Aug 21 '18 at 18:47
  • @esqew I'm used to doing this in PHP with preg_match_all. Switching over to JavaScript made doing this a lot harder. Is there a way to do this in JavaScript? – frosty Aug 21 '18 at 19:16
  • You can address your issue by utilizing the solution in the duplicate question by adding the `?` quantifier after your `.+` tokens. [regex101 test](https://regex101.com/r/XUkF2W/1) – esqew Aug 21 '18 at 19:22

1 Answers1

-2

text.match(/(?<=user test)(.*)(?=user test)/g)

melvil james
  • 592
  • 7
  • 18