如何使用Regex验证段落中的URL



我正在使用以下regex来验证URL:

/^(?:(?:https?|ftp)://)?(?:(?!(?:10|127)(?:.d{1,3}){3})(?!(?:169.254|192.168)(?:.d{1,3}){2})(?!172.(?:1[6-9]|2d|3[0-1])(?:.d{1,3}){2})(?:[1-9]d?|1dd|2[01]d|22[0-3])(?:.(?:1?d{1,2}|2[0-4]d|25[0-5])){2}(?:.(?:[1-9]d?|1dd|2[0-4]d|25[0-4]))|(?:(?:[a-zu00a1-uffff0-9]-*)*[a-zu00a1-uffff0-9]+)(?:.(?:[a-zu00a1-uffff0-9]-*)*[a-zu00a1-uffff0-9]+)*(?:.(?:[a-zu00a1-uffff]{2,})))(?::d{2,5})?(?:/S*)?$/

但我无法在这样的段落中验证URL:

许多学生根据长度来定义段落:一个段落是由至少五个句子组成的一组,一个段落有半页长,等等。然而,实际上,u www.example.com和句子之间思想的连贯性构成了一个段落。

此处无法检测到example.com

在这种情况下,有人能提供正则表达式来验证URL吗?

关于RegEx,您需要了解一些事情。

^-是字符串开始的锚点。

$-是字符串末尾的锚点。

当您使用/^some regex$/时,只有当字符串是"some regex"而不是其他任何东西时,它才会匹配。("some regex"事件,由于开始时有空格,将不匹配(

let str = "Many students define paragraphs in terms of length: a paragraph is a group of at least five sentences, a paragraph is half a page long, etc. In reality, though, the u www.example.com and and coherence of ideas among sentences is what constitutes a paragraph."
let reg = /(?:(?:https?|ftp)://)?(?:(?!(?:10|127)(?:.d{1,3}){3})(?!(?:169.254|192.168)(?:.d{1,3}){2})(?!172.(?:1[6-9]|2d|3[0-1])(?:.d{1,3}){2})(?:[1-9]d?|1dd|2[01]d|22[0-3])(?:.(?:1?d{1,2}|2[0-4]d|25[0-5])){2}(?:.(?:[1-9]d?|1dd|2[0-4]d|25[0-4]))|(?:(?:[a-zu00a1-uffff0-9]-*)*[a-zu00a1-uffff0-9]+)(?:.(?:[a-zu00a1-uffff0-9]-*)*[a-zu00a1-uffff0-9]+)*(?:.(?:[a-zu00a1-uffff]{2,})))(?::d{2,5})?(?:/S*)?/gi
let op = str.match(reg, str);
console.log(op);

你可以在这里阅读更多关于这方面的内容。https://www.regular-expressions.info/refanchors.html

相关内容

  • 没有找到相关文章

最新更新