当替换字符串包含$'个字符时,请用奇怪的字符串替换方法结果。
示例:https://jsfiddle.net/1jns2eo9/
var p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
var regex = /dog/gi;
document.getElementById('first').innerHTML = p.replace(regex, '$');
/// The quick brown fox jumps over the lazy $. If the $ reacted, was it really lazy?
document.getElementById('second').innerHTML = p.replace(regex, "$'");
/// The quick brown fox jumps over the lazy . If the dog reacted, was it really lazy?. If the reacted, was it really lazy? reacted, was it really lazy?
郎朗坤
相关分类