Javascript 如何用正则表达式将文本中的所有点替换为空格,但不包括URL?

Javascript 如何用正则表达式将文本中的所有点替换为空格,但不包括URL?,javascript,regex,Javascript,Regex,我拥有的正则表达式: (?<!www)(\.+) Dot. www.site.org word. Dot. www.site.site.org word. Dot. www.site.site.org word. D.C. (? 您可以使用类似regex的正则表达式将URL以外的所有点块替换为一个空格 text=text.replace(/((?:https?:\/\/\/\\/\\\\\/\\\\\\\/\\\\\\.+/g,函数(x,y){ 返回y?y:“”; }) 见 关键是

我拥有的正则表达式:

(?<!www)(\.+)

Dot. www.site.org word.
Dot. www.site.site.org word.
Dot. www.site.site.org word. D.C.
(?


您可以使用类似regex的正则表达式将URL以外的所有点块替换为一个空格

text=text.replace(/((?:https?:\/\/\/\\/\\\\\/\\\\\\\/\\\\\\.+/g,函数(x,y){
返回y?y:“”;
})

关键是匹配URL并将其捕获到捕获组中,匹配所有其他点而不捕获,并替换为指向捕获组值的反向引用

这种方法适用于所有浏览器,包括Safari和IE

请参见JavaScript演示:

var text=“Dot.www.site.org word.\nDot.www.site.site.org word.\nDot.www.site.site.org word.D.C.”;
text=text.replace(/((?:https?:\/\/\/\www\)\S*)\。+/g,函数(x,y){
返回y?y:“”;
});

console.log(text);
假设您的URL以
http://
https://
www.
开头,您可以在Javascript中使用此正则表达式,并带有负查找:

(?

代码:

repl = str.replace(/(?<!\b(?:https?:\/\/|www)\S*)\./g, ' ')
repl=str.replace(/(?)?