简体   繁体   English

正则表达式:替换; 从单词,如果单词不以&开头

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM