Javascript replace method

A few things about js replace method:

var anotherstr=str.replace(/<[^<>]+>/g, function(m) {
return m.replace( /__i__|%i%/g, n );});

First, str.replace does not change str itself, the result is saved in anotherstr.

Second, ^ in regex [] does not mean “beginning”, but “not”. “g” means global, for every match, the callback function is called with the matched string as the paramater m. The function returns a string used to replace the matched string. “|” in the regex // means “or”.

Leave a Reply