Search and replace in javascript

Hi

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

var mylink = “mens-shirts.new-arrivals”;

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 = "mens-shirts.new-arrivals";
var newlink = mylink.replace(/\\..*$/,".price-high-low");
alert(newlink);

Alternatively, you could abstract this into a function.
E.g.

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