4

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

"aaavddv".match(/(?:v).*(?:v)/)
Handsome Nerd
  • 15,997
  • 20
  • 92
  • 166

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,502
  • 20
  • 29
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
  • 115,126
  • 25
  • 163
  • 167
3

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

ravi
  • 3,144
  • 6
  • 24
  • 27