var a = 'Construction,Airports,Construction|Commercial Construction,Construction|Education,Construction|Healthcare,Construction|Housing,Construction|Industrial Construction,Construction|Other,Construction|Ports,Construction|Rail,Construction|Residential Construction,Construction|Roads & Bridges,Social Infrastructure|Commercial Construction,Social Infrastructure|Education,Social Infrastructure|Healthcare,Social Infrastructure|Housing,Social Infrastructure|Other,Social Infrastructure|Residential Construction';
alert(a.replace('|', ',', 'g'));在chrome上,它只替换|的第一次出现,同时在replace()函数的regex形式中使用g标志,它替换所有事件:
alert(a.replace(/\|/g, ',', 'g'));,谁能帮我理解一下,如果我在第一种形式的替换中做错了什么?这是预期的行为还是一个bug?
发布于 2013-09-11 14:35:08
使用此格式:a.replace(/\|/g, ',') jsFiddle实例
根据MDN
在String.replace方法中使用标志参数是不标准的。不要使用此参数,而是使用带有相应标志的RegExp对象。
https://stackoverflow.com/questions/18744024
复制相似问题