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 上,它仅替换 的第一次出现|
,而g
在函数的正则表达式形式中使用标志时replace()
,它会替换所有出现:
alert(a.replace(/\|/g, ',', 'g'));
任何人都可以帮助我了解我在替换的第一种形式中是否做错了什么?这是预期的行为还是错误?