javascript - XRegExp no look-behind? -
i need use lookbehind of regex in javascript, found simulating lookbehind in javascript (take 2). also, found author steven levithan 1 developed xregexp.
i git cloned xregexp 3.0.0-pre, , tested
some lookbehind logic http://regex101.com/r/xd0xz5 using xregexp
var xregexp = require('xregexp'); console.log(xregexp.replace('foobar', '(?<=foo)bar', 'test'));
it seems not working;
$ node test foobar
what miss? thanks.
edit: goal like
(?<=foo)[\s\s]+(?=bar)
(edit2 link wrong , modifed)
answer:
var str = "fooanythingbar"; console.log(str); console.log(str.replace(/(foo)(?:[\s\s]+(?=bar))/g, '$1test')); //footestbar
credit goes @trevor senior thanks!
it possible use non-capturing groups this, e.g.
$ node > 'foobar'.replace(/(foo)(?:bar)/g, '$1test') 'footest'
in second parameter of string.replace, special notation of $1
references first capturing group, (foo)
in case. using $1test
, 1 can think of $1
placeholder first matching group. when expanded, becomes 'footest'
.
for more in depth details on regular expression, view matches here.
Comments
Post a Comment