Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js detect link in string

function replaceURLs(message) {
  if(!message) return;
 
  var urlRegex = /(((https?://)|(www.))[^s]+)/g;
  return message.replace(urlRegex, function (url) {
    var hyperlink = url;
    if (!hyperlink.match('^https?://')) {
      hyperlink = 'http://' + hyperlink;
    }
    return '<a href="' + hyperlink + '" target="_blank" rel="noopener noreferrer">' + url + '</a>'
  });
}
 
replaceURLs("Visit www.cluemediator.com and subscribe us on https://www.cluemediator.com/subscribe for regular updates.")
// Output: Visit <a href="http://www.cluemediator.com" target="_blank" rel="noopener noreferrer">www.cluemediator.com</a> and subscribe us on <a href="https://www.cluemediator.com/subscribe" target="_blank" rel="noopener noreferrer">https://www.cluemediator.com/subscribe</a> for regular updates.
Comment

PREVIOUS NEXT
Code Example
Javascript :: fetch api javascript 
Javascript :: boilerplate node js server 
Javascript :: getting all the selected text from multiselect and joing them. 
Javascript :: javascript assignment operators 
Javascript :: loop every 5 seconds 
Javascript :: owl.js cdn 
Javascript :: how to set background colour i js inline stylel 
Javascript :: jquery window offset top 
Javascript :: viewchild for ngfor 
Javascript :: disable a button in javascript 
Javascript :: return fetch javascript 
Javascript :: javascript allow only numeric characters 
Javascript :: add property to object conditionally 
Javascript :: change the mouse pointer javascript 
Javascript :: string contains string javascript 
Javascript :: redux devtools extention in redux toolkit 
Javascript :: closest javascript 
Javascript :: javascript rupiah currency format 
Javascript :: local storage remove multiple items 
Javascript :: delete a property of html by js 
Javascript :: add css on click javascript 
Javascript :: how to access key of object in javascript 
Javascript :: settimeout javascript see how much time is left 
Javascript :: deploy react js heroku 
Javascript :: substring javascript 
Javascript :: addeventlistener js select item 
Javascript :: discord.js send message to a given channel 
Javascript :: how to disable keyboard calender input in material ui datepicker reactjs 
Javascript :: how to generate random number in javascript 
Javascript :: is object js 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =