Replace global

Regular expressions are a powerful tool for performing pattern matches in Strings in JavaScript.

var s ='text in text in text';
var a = text;
var b = link;
var search = new RegExp(a, 'g');
s = s.replace(search, b);

out : link in link in link