gpt4 book ai didi

javascript - 在javascript中使用正则表达式替换字符的奇数出现

转载 作者:行者123 更新时间:2023-11-30 09:59:37 25 4
gpt4 key购买 nike

我有一个包含“#”的字符串。我想用其他东西替换仅出现的奇数“#”。

例如对于下面的字符串

This is my string # This is my string # This is my string # This is my string #

输出应该是

This is my string # This is my string '' This is my string # This is my string ''

Note : considering occurrence starts with 0

最佳答案

([^#]*#[^#]*)#

您可以使用它。替换为 $1"。查看演示。

https://regex101.com/r/cT0hV4/9

var re = /([^#]*#[^#]*)#/gm; 
var str = 'This is my string # This is my string # This is my string # This is my string #';
var subst = '$1"';

var result = str.replace(re, subst);

关于javascript - 在javascript中使用正则表达式替换字符的奇数出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32370777/

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