gpt4 book ai didi

Javascript 使用正则表达式替换函数

转载 作者:行者123 更新时间:2023-12-01 11:42:16 25 4
gpt4 key购买 nike

我正在尝试使用 javascript 的替换函数来替换字符串。但它只是取代了第一个实例。所以当我使用正则全局表达式时,

var result = 'moaning|yes you|hello test|mission control|com on'.replace(/|/g, ';');

我得到:http://jsfiddle.net/m8UuD/196/

我想得到:

moaning;yes you;hello test;mission control;com on

最佳答案

简单地逃避管道:

 'moaning|yes you|hello test|mission control|com on'.replace(/\|/g, ';');

Here you'll find the list of regex special characters that you should generally escape .

关于Javascript 使用正则表达式替换函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15842051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com