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
Post a Comment