Why doesn't the javascript replace global flag work in Chrome or IE, and how to I work around it?

前端 未结 3 537
栀梦
栀梦 2021-01-01 16:57

According to the String.prototype.replace() page on MDN, I should be able to easily replace multiple patterns just by using

str.replace(\'what to replace\',          


        
3条回答
  •  有刺的猬
    2021-01-01 17:19

    It appears that webkit's implementation of string.replace perhaps doesn't have the 3rd parameter, as 'foo'.replace('o','i','g') results in fio for me.

    The following appears to work however:

    'foo'.replace(/o/gi,'i')
    

    Another option is:

    'foo'.replace(new RegExp('o', 'gi'),'i')
    

提交回复
热议问题