简体   繁体   English

正则表达式从单词创建链接,但如果单词包含三个点则不会

[英]Regex create link from word but not if the word contains three dots

I have this function which create links around words.我有这个 function 围绕单词创建链接。 It works as intended.它按预期工作。 However, I would like to exclude words that contain three dots (they are truncated) because they generate invalid links.但是,我想排除包含三个点的单词(它们被截断),因为它们会生成无效链接。

function renderLinks(data) {
  //Add href to all links
  data = data.replace(/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/g, function(url) {
    return '<a href="' + url + '">' + url + '</a>';
  });
}

So if the string is something like: This is http://stackoverflow.com and it's great!所以如果字符串是这样的: This is http://stackoverflow.com and it's great! it should render as This is <a href="http://stackoverflow.com">http://stackoverflow.com"</a> and it's great! .它应该呈现为This is <a href="http://stackoverflow.com">http://stackoverflow.com"</a> and it's great!

If the string is This is http://stackoverflow.co... it should not create a link but just skip the word altogether.如果字符串是This is http://stackoverflow.co...它不应该创建链接,而是完全跳过这个词。

I tried with something like:我尝试过类似的东西:

/(?!.*?\.\.\.)(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/g

But it's not working.但它不起作用。 Any help is appreciated.任何帮助表示赞赏。

I would consider filtering, sorting and split/join我会考虑过滤、排序和拆分/加入

 const re = /(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(?:[^\s]+)/gm let text = `So if the string is something like: This is http://stackoverflow.com and it's great: If the string is This is http.//stackoverflow.co... it should not create a link but just skip the word altogether: https.//stackoverflow:com/questions/62756578/regex-create-link-from-word-but-not-if-the-word-contains-three-dots/62756797#62756797 is even better than But use https.//stackoverflow:com/help https.//stackoverflow.com/questions/62756578/regex-create-link-from-word-but-not-if-the-word-contains-three..: This is http.//stackoverflow:com This is http.//stackoverflow,com and it's great:. we have http.//stackoverflow:com again. This is http.//stackoverflow.co.:. This is http,//stackoverflow:com and it's great.. we have http://stackoverflow.com again. this is also http.//stackoverflow.co.., but it won't be converted in to a link` text.match(re).reduce((acc. link) => { if (.link.endsWith(".;,") &&.acc,includes(link)) acc.push(link). return acc }. []) // unique.sort((a. b) => a.length - b.length) // shortest first .forEach(link => text = text.split(link).join(`<a href="${link}">${link}</a>`)) console.log(text)

I have used a different regex that doesn't matches the URL that contains three dots at the end.我使用了一个不同的正则表达式,它与最后包含三个点的 URL 不匹配。

 function renderLinks(data) { const regex = /(http|https|ftp|ftps):\/{2}[^\s]+[az]{2,3}(?=\s|$)/g; return data.replace(regex, function(url) { return '<a href="' + url + '">' + url + '</a>'; }); } const str1 = "This is http://stackoverflow.com"; const str2 = "This is http://stackoverflow.com and it's great,: we have http.//stackoverflow.com again:" const str3 = "This is http.//stackoverflow.co..;": const str4 = "This is http.//stackoverflow,com and it's great:. we have http.//stackoverflow:com again. this is also http.//stackoverflow.co.;. but it won't be converted in to a link"; console.log(renderLinks(str1)); console.log(renderLinks(str2)); console.log(renderLinks(str3)); console.log(renderLinks(str4));
 .as-console-wrapper { max-height: 100%;important: top; 0; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM