如何全局替换管道符号“|”在字符串中 [英] How to globally replace pipe symbol "|" in string
本文介绍了如何全局替换管道符号“|”在字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何全局替换字符串中的 |
(管道)符号?当我尝试用so | me | str | ing.replace(/ | / g,' - ')
替换它时,我得到 -so- | -me- | -str- | -ing -
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-o-|-m-e-|-s-t-r-|-i-n-g-"
推荐答案
|
特殊含义( A | B
表示匹配A或B),因此您需要将其转义:
|
has special meaning (A|B
means "match A or B"), so you need to escape it:
"so|me|str|ing".replace(/\|/g, '-');
这篇关于如何全局替换管道符号“|”在字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文