4

Why following return ["vddv"] instead of ["dd"]:

"aaavddv".match(/(?:v).*(?:v)/)
Handsome Nerd
  • 17,114
  • 22
  • 95
  • 173

3 Answers3

4
(?:v) # matches 'v' this is a non-capturing group, not a lookbehind
.*    # matches 'dd'
(?:v) # matches 'v' this is a non-capturing group, not a lookahead

Non-capturing groups still participate in the match. Perhaps you want a lookahead/behind? But Javascript does not support lookbehind.

alan
  • 4,752
  • 21
  • 30
3
"aaavddv".match(/(?:v)(.*)(?:v)/)[1]

the whole match is correctly vddv but if you want to match only dd you need to use a capturing group (and look at element [1])

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
3

/(?:v).*(?:v)/ specifies expression v(number of characters)v

ravi
  • 3,304
  • 6
  • 25
  • 27