search

Home  >  Q&A  >  body text

When JavaScript executes the string replace method, there is a strange phenomenon about $.

When replacing string replacement, if the new content is two consecutive $$, it will become 1 when output.

var str = '哈哈a哈哈a哈哈哈';
var re = str.replace('a','****');
console.log(re);
//结果是:哈哈****哈哈a哈哈哈
            
var str = '哈哈a哈哈a哈哈哈';
var re = str.replace('a','$$$$');
console.log(re);
//结果是:哈哈$$哈哈a哈哈哈
//4个$变成了2个,为什么
巴扎黑巴扎黑2805 days ago498

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-05-19 10:38:47

    `
    $ has special meaning in replace, just like for strings, the first transfer, $$ represents the character $
    `

    reply
    0
  • Cancelreply