My JS: var objLink = ...">

Find text in a string and replace with another in JavaScript

My html:

<span data-sample="http://mysite.com"></span> 

My JS:

  var objLink = $(this).attr('data-sample'); 

Hence my objLink == "http://mysite.com"

How can I add "www" and make objLink == "http://www.mysite.com" ?

+4
source share
1 answer

Not sure if this is the answer you are looking for, but you can use .replace() for this.

 objLink = objLink.replace("http://", "http://www."); 

Check out the demo: http://jsfiddle.net/XZKaj/

+2
source

Source: https://habr.com/ru/post/1394550/


All Articles