當替換字符串包含$'個字符時,請用奇怪的字符串替換方法結(jié)果。示例: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?
如果替換具有$',則replace方法會產(chǎn)生意外結(jié)果
不負相思意
2021-05-16 12:13:09