[英]Regex Match All Except Leading and Trailing Spaces Around Commas (including comma)
我正在尝试匹配除逗号及其周围的所有空格以外的所有内容 。 但是,我希望它匹配单词之间的空格。 这是我到目前为止的内容:
/[^,\s][a-zA-Z0-9_ !@#$%^&*()]+/gi
它匹配除逗号和尾部空格以外的所有内容,但包括前导空格。 这是测试regex101的链接。
我正在尝试围绕关键术语包装跨度标签。 这就是我希望最终结果是:
<span>Some text</span> , <span>Some more text</span>
我目前正在使用
.replace(/([^,\s][a-zA-Z0-9_ !@#$%^&*()]+)/gi, "<span>$1</span>")
并在contenteditable div中更新span标签。 我正在使用它通过CSS在关键术语周围放置气泡,并在每次输入后进行更新。 任何帮助将不胜感激! 谢谢!
正则表达式为:
([\s\S]+?)(\s*,\s*|$)
然后在replace()
方法中向后引用两个捕获组:
const regex = /([\\s\\S]+?)(\\s*,\\s*|$)/g; const str = `uncased is a test , vertical this is a tests 80000 , bertical 80000 , vertical #303 cardio endo, kljafklj\\$!asdfkj`; // The substituted value will be contained in the result variable const result = str.replace(regex, '<span>$1</span>$2'); console.log(result);
这能达到目的吗?
/((?:(\w|[-!@#$%^&*()])+( (\w|[-!@#$%^&*()]))*)+)/g
要么
/((?:(\w|[-!@#$%^&*()])+( {1,MAX_NUMBER_OF_SPACES}(\w|[-!@#$%^&*()]))*)+)/g
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.