[英]Regex : Replace ; from word if word does not start with &
I'm trying to use Regex in javascript. 我正在尝试在JavaScript中使用正则表达式。 Lets take this scenario,
让我们看看这个场景,
str = "This; is; John; & Jane;"
The result I need, 我需要的结果,
str= "This* is* John* & Jane*"
This is what I have tried, 这是我试过的,
str.replace(/\^(?!&)\w+;\s/g, "*");
Please help. 请帮忙。 Thank you.
谢谢。
Try 尝试
str.replace(/(^|\s)([^&]\S+?);(?=$|\s)/g, "$1$2*")
You cannot do that without capturing groups because that would require a lookbehind assertion, which Javascript doesn't support. 如果没有捕获组,你就无法做到这一点,因为这需要一个jaw不支持的lookbehind断言。
试试这个:
str.replace(/((^| )[^&]\w+?);/g, "$1*");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.