1

I am trying to get a results that shows multiple matches.. but this regex only shows one.

var regexPattern = /(^.+\/)/g;

var string2Test = 'this/should/have/several';

var myMatches = string2Test.match(regexPattern);

console.log(myMatches);

should it not match the three below?

this/
this/should/
this/should/have/

I have been testing it here. https://regex101.com/r/nD7nP6/36

  • 1
    No, it should not. – Wiktor Stribiżew Jun 29 '17 at 21:33
  • 1
    It will only match once because of two reasons. The BOS anchor `^` and the greedy quantifier `.*`. –  Jun 29 '17 at 21:43
  • Change it to `/([^/]*)\//` https://regex101.com/r/nD7nP6/37 –  Jun 29 '17 at 21:44
  • You could get 3 matches like you state, but you'd have to use 3 separate regex's. What is it you're trying to do exactly ? –  Jun 29 '17 at 21:48
  • my goal is to identify all the valid folders in a URL. So a regex that says [start of string]+[any characters including a slash] + [a forward slash]. I want all three examples above to match. If you test each of the three matches I list above separately.. they are all a match. https://regex101.com/r/nD7nP6/38 https://regex101.com/r/nD7nP6/39 https://regex101.com/r/nD7nP6/40 – David Carter Jul 04 '17 at 15:02
  • Possible duplicate of [How can I match overlapping strings with regex?](https://stackoverflow.com/questions/20833295/how-can-i-match-overlapping-strings-with-regex) – LarsW Aug 15 '17 at 14:17

0 Answers0