正则表达式查找不遵循 http:// 或 https:// 的网站名称

Regex to find web site names which does not follow http:// or https://

我需要找到正则表达式来查找不遵循 http:// 或 https:// 的网站名称 例如

http://www.google.co.in  ---dont match
http://www.google.co.in  ---dont match
www.google.co.in         ---match

URL 也可以是更大字符串的一部分,例如

<p><a href="https://www.w3schools.com/html/">www.w3schools.com</a></p>

The URL To be Matched is www.w3schools.com and www.abc.com , URL Not to be matched is https://www.w3schools.com/html/

其中 www.w3schools.comwww.abc.com (在第二个例子中)应该匹配,字符串中可以有多个 url

提前致谢

如果您只想排除http://https:// 开头的字符串,这很容易通过否定前瞻来完成:

var match = "www.google.co.in";
var nomatch = "http://www.google.co.in";

var re = new RegExp("^(?!https?:\/\/).*$");
if (re.test(match)) {
    console.log(match + " is valid");
}
if (re.test(nomatch)) {
    console.log(nomatch + " is valid");
}

这种模式的一个优点是它还允许在其他条件下过滤正匹配 URL。

您可以使用正则表达式 ^(http|https):// 来匹配具有 http://https:// 的字符串。然后,当您应用匹配时,使用 not (!) 运算符将匹配反转为不包含 http://https://:

var regEx = new RegExp("^(http|https)://", "i");
var str = "http://www.google.co.in";
var match = !regEx.test(str);
console.log(match + ' for ' + str);

str = 'http://www.google.co.in';
match = !regEx.test(str);
console.log(match + ' for ' + str);

str = 'www.google.co.in';
match = !regEx.test(str);
console.log(match + ' for ' + str);

你需要吗?

/(?<!https:\/\/)(?<!http:\/\/)(www\.[\w-.]*?[\w-]+?(\/[\w-]*?)*?)((?=[^\w.\/-]+?)|$)+/ig

你可以看这里:

https://regex101.com/r/XvmR4V/4

如果您有一个包含网站名称的大字符串,此正则表达式会匹配所有不以 "http://" 或 "https://" 开头的名称。您的网站名称必须始终以 "www"!!!

开头

如果没有先行和后行,你可以试试这个。 您将在 2. 组 ($2).

中找到结果
/([^\/]{2,2})(www\.[\w-.]*?[\w-]+?(\/[\w-]*?)*?)(([^\w.\/-]+?)|$)+/ig

https://regex101.com/r/XvmR4V/5

现在 www.google.de:

([^\/]{2,2}|^)(www\.[\w-.]*?[\w-]+?(\/[\w-]*?)*?)(([^\w.\/-]+?)|$)+

https://regex101.com/r/XvmR4V/6

你可以这样替换。

我用 'Test' 替换了 'www...'。

/([^\/]{2,2}|^)(www\.[\w-.]*?[\w-]+?(\/[\w-]*?)*?)(([^\w.\/-]+?)|$)+/Test/gi

我使用 IntelliJ 的正则表达式工具对其进行了测试。

我的输入是:

<p><a href="https://www.w3schools.com/html/"><a href="http://www.w3schools.com/html/">www.w3schools.com</a></p>
<p><a href="https://www.google.com/html/"><a href="http://www.google.com/html/">www.google.com</a>

输出是:

<p><a href="https://www.w3schools.com/html/"><a href="http://www.w3schools.com/html/">Test</a></p>
<p><a href="https://www.google.com/html/"><a href="http://www.google.com/html/">Test</a>

如果有帮助,请投赞成票:-)