K20GH
K20GH

Reputation: 6271

url.replace with multiple replacements?

I've currently got the following to remove spaces and replace them with a hyphen. How can I go about replacing a space with a hyphen and a dot with nothing and keep it in the same variable?

url = url.replace(/\s/g, '-');

Upvotes: 1

Views: 2922

Answers (2)

Thomas Anderson
Thomas Anderson

Reputation: 38

I use this:

// This little gadget does replace for all not just first occurence like the native javascript function.
String.prototype.replaceAll = function(strTarget, strSubString){
  var strText = this;
  var intIndexOfMatch = strText.indexOf(strTarget);
  while (intIndexOfMatch != -1){
    strText = strText.replace(strTarget, strSubString);
    intIndexOfMatch = strText.indexOf(strTarget);
  }
  return(strText);
}

Upvotes: 2

user827080
user827080

Reputation:

url = url.replace(/\s/g, '-').replace(/\./g, ''); might do it.

Upvotes: 4

Related Questions