1

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

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

2 回答 2

3

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

于 2012-10-07T11:42:47.160 回答
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);
}
于 2012-10-07T11:43:55.980 回答