url.replace有多个替换?

时间:2012-10-07 11:39:08

标签: javascript jquery

我目前有以下内容删除空格并用连字符替换它们。 如何用一个带有连字符和点的空格替换空格并将其保存在同一个变量中?

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

2 个答案:

答案 0 :(得分:3)

url = url.replace(/\s/g, '-').replace(/\./g, '');可能会这样做。

答案 1 :(得分:2)

我用这个:

// 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);
}
相关问题