简体   繁体   中英

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

I have this function which create links around words. 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! it should render as 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.

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.

 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; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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