我有一个正则表达式,我尝试找到所有网址来创建链接((,但我有以下问题:
- 一些带有"\foo\bar"的网址没有得到它们
- URL 将其作为网络的一部分。
www.foo.com -> https://mywebsite.com/section/www.foo.com
如果是没有https的链接,则可能会自动放置(避免FTP或\主机名或\ IP(???
谢谢!
实时正则表达式: https://regex101.com/r/w3o9w1/1
正则表达式:
/(?:(?:https?|ftp|)://|b(?:[a-zd]+.))(?:(?:[^s()<>]+|((?:[^s()<>]+|(?:([^s()<>]+)))?))+(?:((?:[^s()<>]+|(?:(?:[^s()<>]+)))?)|[^s`!()[]{};:'".,<>?«»“”‘’]))?/gm
文本演示:
text www.example.com text text text http://example.com
\hadgs01test2
http://192.168.1.1:3000/
192.168.1.1:3000
\192.168.10.10testtest.txt
http://example.com
http://example.gl
http://www.example.com
https://example.com
https://www.example.com
https://www.example.com/
https://www.example.com/bar
https://example.com/icons?d=bar&q=bar
http://abc.dec.ed.example.com
http://example.gl/1 http://example.gl/2
foo (http://example.gl/1) http://example.gl/(2)
http://example.com/. http://example.com/! http://example.com/,
example.gl/1
http://example.com/review/abc-def-ghi/?ct=t(test_test_bar)
www.example.com.au
http://www.example.com.au
http://www.example.com.au/ersdfs
http://www.example.com.au/bar?dfd=test@s=1
http://www.example.com:81/bar.html
法典:
var regex_links = /(?:(?:https?|ftp)://|b(?:[a-zd]+.))(?:(?:[^s()<>]+|((?:[^s()<>]+|(?:([^s()<>]+)))?))+(?:((?:[^s()<>]+|(?:(?:[^s()<>]+)))?)|[^s`!()[]{};:'".,<>?«»“”‘’]))?/gm;
$(".text_to_replace").html($(".text_to_replace").html().replace(regex_links, " <a href="$&" target='_blank'>$&</a> "));
您的正则表达式非常接近您的期望; 但是为了匹配您的要求的字符串(匹配"\\hadgs01\test2"或"\\192.168.10.10\test\test.txt">(,您可以使用以下正则表达式:
(?:(https?|ftp)?:?//|b(?:[a-zd]+.))(?:(?:[^s()<>]+|((?:[^s()<>]+|(?:([^s()<>]+)))?))+(?:((?:[^s()<>]+|(?:(?:[^s()<>]+)))?)|[^s`!()[]{};:'".,<>?«»“”‘’]))?
在正则表达式中所做的更改:
(?:https?|ftp|):
更改为(https?|ftp|)?:?
- 我对你的正则表达式进行了以下修改,以便捕获你的第二个要求的组,并匹配所需的字符串\hadgs01test2
和\192.168.10.10testtest.txt
。
在 JAVASCRIPT 中的实现:
const myRegexp = /(?:(https?|ftp)?:?//|b(?:[a-zd]+.))(?:(?:[^s()<>]+|((?:[^s()<>]+|(?:([^s()<>]+)))?))+(?:((?:[^s()<>]+|(?:(?:[^s()<>]+)))?)|[^s`!()[]{};:'".,<>?«»“”‘’]))?/gm;
const myString = `
text www.example.com text text text http://example.com
//hadgs01/test2
http://192.168.1.1:3000/
192.168.1.1:3000
\\192.168.10.10\test\test.txt
http://example.com
http://examp.ele
http://www.example.com
https://example.com
https://www.example.com
https://www.example.com/
https://www.example.com/bar
https://example.com/icons?d=bar&q=bar
http://abc.dec.ed.example.com
http://examp.le/1 http://examp.ele/2
foo (http://examp.ele/1) http://examp.ele/(2)
http://example.com/. http://example.com/! http://example.com/,
examp.le/1
http://example.com/review/abc-def-ghi/?ct=t(test_test_bar)
www.example.com.au
http://www.example.com.au
http://www.example.com.au/ersdfs
http://www.example.com.au/bar?dfd=test@s=1
http://www.example.com:81/bar.html
\ example \\
`;
// PLEASE NOTE I REPLACED FOO and goo.gle from the string to example and examp.ele because of the norms
let match;
// Taken the below variable to store the result
let resultString = "";
match = myRegexp.exec(myString);
while (match != null) {
// If group 1 of match is null that means it does'nt contain anything among https, http or ftp but it match rest of the requirement.
if(match[1] == null)
// If in case the link already contains slashes like //hadgs01/test2
if(match[0].includes('//'))
resultString = resultString.concat("https:" + match[0] + "n");
else
resultString = resultString.concat("https://" + match[0] + "n");
else
resultString = resultString.concat(match[0] + "n");
match = myRegexp.exec(myString);
}
console.log(resultString);