JavaScript: equivalent regex as PHP -


i have regular expression find next word of given word 'on' in string php:

(?<=\bon\s)(\w+) 

it works php js gives following error:

uncaught syntaxerror: invalid regular expression: /(?<=\bon\s)(\w+)/: invalid group

what equivalent regex javascript?

(?<=\bon\s) positive lookbehind. php's regular expression engine (pcre) supports those, javascript's regular expression engine doesn't.

while it's not possible write similar regex, can still achieve using following regex:

\bon\s(\w+) 

unlike in original regex, \bon\s consumes characters. can still extract results using capturing group (\w+).

usage:

var str = 'foo on bar'; var matches = str.match(/\bon\s(\w+)/); var result = matches[1] // bar 

fiddle


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -