javascript string.replace $1 if it is a certain value? -


is there way check value of $1 , if equal "left" replace "newleft"?

something this: str.replace(re, 'blah blah $2, $1="left" ? "newleft": "something" ');

if pass callback function second argument of the string place method, captured strings regular expression passed arguments callback function. can use these arguments return new string, , perform necessary logic on captured strings.

example 1:

'left, right'.replace(/(.*),\s(.*)/, function(match, p1, p2) {     return 'blah blah ' + p2 + ', ' + (p1 === 'left' ? 'newleft' : 'something'); }); 

response:

"blah blah right, newleft" 

example 2:

'top, right'.replace(/(.*),\s(.*)/, function(match, p1, p2) {     return 'blah blah ' + p2 + ', ' + (p1 === 'left' ? 'newleft' : 'something'); }); 

response:

"blah blah right, something" 

Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -