Search and replace in javascript


Could some help me with a javascript search and replace function…

var mylink = “”;

I just need to able to replace what comes after the “.” with something else. eg. I want to end up with mylink being “mens-shirts.price-high-low” or "mens-shirts.-a-z and so on.

So just a generic function where I can input the new value each time.

Thanks very much

Hi there,

use JavaScript’s replace() function with a regular expression.
For example:

var mylink = "";
var newlink = mylink.replace(/\\..*$/,".price-high-low");

Alternatively, you could abstract this into a function.

function myReplace(str, rep){
  return(str.replace(/\\..*$/, "." + rep));
var mylink = "";
var newlink = myReplace(mylink, "price-high-low");