gpt4 book ai didi

Javascript 正则表达式问题 : strip special symbols and URL's

转载 作者:行者123 更新时间:2023-11-30 18:46:37 24 4
gpt4 key购买 nike

我需要的是能够匹配包含特殊符号(除了 ?、! 和 .)和 URL 的单词。这是我已有的,但效果不佳。

re = new RegExp('(http://\S+|\S*[^\w\s,.":]\S*)');

text = '@hello how are you? http://example.com';
clean = text.replace(re, '');

当前输出为:

> 'hello how are you? http://example.com'

输出应该是:

> '  how are you? '

最佳答案

你需要在 RE 中使用 global

text.replace(/(http:\/\/\S+|\S*[^\w\s,.":?]\S*)/g,'');

我还加了“?”到你的集合,因为你似乎想要包括在内。

关于Javascript 正则表达式问题 : strip special symbols and URL's,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5246809/

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