How to globally replace pipe symbol “|” in string

前端 未结 5 1352
北恋
北恋 2020-12-06 05:25

How can I globally replace the | (pipe) symbol in a string? When I try to replace it with \"so|me|str|ing\".replace(/|/g, \'-\'), I get \"-s-

5条回答
  •  悲&欢浪女
    2020-12-06 06:00

    | has special meaning (A|B means "match A or B"), so you need to escape it:

    "so|me|str|ing".replace(/\|/g, '-');
    

提交回复
热议问题