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

node.js - How to mock a third-party api calls in the backend -

node.js - Why do I get "SOCKS connection failed. Connection not allowed by ruleset" for some .onion sites? -

matlab - 0-by-1 sym - What do I need to change in order to get proper symbolic results? -