【问题标题】:Regex Detect urls and make the link <a>正则表达式检测 url 并使链接 <a>
【发布时间】:2020-05-28 06:59:18
【问题描述】:

我有一个正则表达式,我试图找到所有的 url 来创建链接 () 但我有以下问题:

  • 某些带有“\foo\bar”的网址无法获取
  • 网址将其作为网络的一部分。

www.foo.com -> https://mywebsite.com/section/www.foo.com

如果是没有https的链接有可能会自动放上去(避免ftp或者\hostname或者\ip)???

谢谢!

实时正则表达式: https://regex101.com/r/w3o9w1/1

正则表达式:

/(?:(?:https?|ftp|):\/\/|\b(?:[a-z\d]+\.))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))?/gm

文字演示:

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://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-z\d]+\.))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))?/gm;

$(".text_to_replace").html($(".text_to_replace").html().replace(regex_links, " <a href=\"$&\" target='_blank'>$&</a> "));

【问题讨论】:

  • @Mandy8055 nope "\\hadgs01\test2" 或 "\\192.168.10.10\test\test.tx" 没有包含
  • @Mandy8055 "192.168.178.11/asd" -> "https://192.168.178.11/asd" 没有忽略 ftp 协议 :/ ,我可以这样做 if($1 != 'ftp') ??
  • @Mandy8055 是的,如果我没有协议,我会给它 https,如果我有 "\\example\" 我什么都不做
  • 我认为有空格时最好忽略。
  • @Mandy8055 我刚刚将它改编为 jquery,如果它适用于我,唯一缺少的是“\\192.168.10.10\test\test.txt”和“\\hadgs01\test2”谢谢!

标签: javascript regex


【解决方案1】:

您的正则表达式非常接近您的预期;但为了匹配您要求的字符串(匹配“\\hadgs01\test2”或“\\192.168.10.10\test\test.txt”),您可以使用以下正则表达式: p>

(?:(https?|ftp)?:?\/\/|\b(?:[a-z\d]+\.))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))?

您的正则表达式中的更改:

(?:https?|ftp|): 更改为 (https?|ftp|)?:? - 我对您的正则表达式进行了以下修改,以便为您的第二次捕获组要求以及匹配所需的字符串类型\\hadgs01\test2 和\\192.168.10.10\test\test.txt。

在 JAVASCRIPT 中的实现:

const myRegexp = /(?:(https?|ftp)?:?\/\/|\b(?:[a-z\d]+\.))(?:(?:[^\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);

【讨论】:

  • /(?:(https?|ftp)?:\/\/|\\\\|\b(?:[a-z\d]+\.))(?:(? :[^\s()]+|((?:[^\s()]+|(?:([^\s()]+)))?))+(? :((?:[^\s()]+|(?:(?:[^\s()]+)))?)|[^\s`!()[]{} ;:'".,?«»“”'']))?/gm
  • 我对正则表达式进行了一些改进,以便在字符串以点结尾时跳过,但是我对里面有点的日期有疑问,你能帮我解决这个问题吗? IP 为 0-255,分 4 个块(从不为 4 个数字)。谢谢 ! regex101.com/r/Y7Ek2b/2 (
  • 在这种情况下;您需要在您的正则表达式中嵌入 IP 验证,因为 Date 在点之后最多可以是 4 位数字(年份);但是这样正则表达式会很麻烦。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
相关资源
最近更新 更多