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 -

java - Could not locate OpenAL library -

Non Unique Username with ASP.Net Identity 2.0 -