2009-11-03 38 views
6

我想從字符串的開始/結尾刪除所有不必要的逗號。在JavaScript中刪除不需要的逗號

例如; google, yahoo,, ,應該成爲google, yahoo

如果可能的話,google,, , yahoo,, ,應該變成google,yahoo

我已經嘗試了下面的代碼作爲一個起點,但它似乎不按預期工作。

trimCommas = function(s) { 
s = s.replace(/,*$/, ""); 
s = s.replace(/^\,*/, ""); 
return s; 
} 

回答

14

在你的榜樣,你也想修剪逗號如果在開始或結束他們之間的空間,使用這樣的:

str.replace(/^[,\s]+|[,\s]+$/g, '').replace(/,[,\s]*,/g, ','); 

的使用注意事項的「G」用於全局替換的修改器。

+0

有人投下了這個答案。你願意解釋嗎?乍看之下,我認爲它沒有問題。 – Boldewyn 2009-11-03 08:44:49

+0

可能是因爲它似乎沒有處理字符串中的「,」,(如果可能的話)? – paxdiablo 2009-11-03 09:21:30

+0

錯過了,對不起。修正了正則表達式來處理這個問題。 – 2009-11-03 09:28:13

1

首先在Google上爲「Javascript修剪」ping:http://www.somacon.com/p355.php。你似乎已經用逗號實現了這一點,我不明白爲什麼它會成爲一個問題(儘管你在第二個而不是第一個中逃脫了)。

+0

謝謝,但它是用於刪除空格不是爲逗號 – 2009-11-03 09:49:28

6

你需要這樣的:

s = s.replace(/[,\s]{2,}/,""); //Removes double or more commas/spaces 
s = s.replace(/^,*/,""); //Removes all commas from the beginning 
s = s.replace(/,*$/,""); //Removes all commas from the end 

編輯:使所有的變化 - 現在應該工作。

+0

幾乎,但不完全。修正了對於雅:-) – paxdiablo 2009-11-03 08:27:38

+0

其實,這仍然有問題,我沒有檢測到 - 它不處理的空間。你可能想解決這個問題。 – paxdiablo 2009-11-03 09:22:44

+0

這很好,除了它不處理空格。 for s =「,chrome,firefox,,, Internet Explorer,safari,opera ,,」; 我得到了「chrome,firefox,Internet Explorer,safari,opera ,,」 – 2009-11-03 09:51:38

3

你需要做的是取代的「空格和逗號」的所有組與一個逗號,然後從開始逗號和結束:

trimCommas = function(str) { 
    str = str.replace(/[,\s]*,[,\s]*/g, ","); 
    str = str.replace(/^,/, ""); 
    str = str.replace(/,$/, ""); 
    return str; 
} 

第一個替換的白色空間的每個序列和逗號和逗號,只要在那裏至少有一個逗號。這處理留在「Internet Explorer」註釋中的邊緣案例。

第二個和第三個在需要時在字符串的開始和結尾處刪除逗號。

您還可以添加(到最後):

str = str.replace(/[\s]+/, " "); 

塌陷多空間下降到一個空間和

str = str.replace(/,/g, ", "); 

,如果你希望他們以後每次(空間很好地格式化逗號)。

的更一般的解決辦法是將參數傳遞給指示行爲:

  • 傳遞truecollapse將摺疊部分內的空間(截面被定義爲逗號之間的字符)。
  • 通過trueaddSpace將使用", "分開部分,而不是自己的","

下面的代碼。對於您的特殊情況可能不需要,但對於其他代碼可能會更好。

trimCommas = function(str,collapse,addspace) { 
    str = str.replace(/[,\s]*,[,\s]*/g, ",").replace(/^,/, "").replace(/,$/, ""); 
    if (collapse) { 
     str = str.replace(/[\s]+/, " "); 
    } 
    if (addspace) { 
     str = str.replace(/,/g, ", "); 
    } 
    return str; 
} 
+0

不是,對於s =「,chrome,firefox,,, Internet Explorer,safari,opera ,,」; 我得到了「鉻,火狐,互聯網,資源管理器,野生動物園,歌劇」 我保持'互聯網'和'探險家'之間的空間 – 2009-11-03 09:53:49

+0

請參閱更新的第一個替換。它現在用逗號代替任何空格和逗號序列,*提供*那裏至少有一個逗號。這應該處理「Internet Explorer」。 – paxdiablo 2009-11-03 10:32:00

+0

很好,它工作正常。 s =「,chrome,firefox,,,,,,Internet Explorer,safari,opera ,,」yield「chrome,firefox,Internet Explorer,safari,opera」我真正想要的 – 2009-11-03 13:24:48

4

我的看法:

var cleanStr = str.replace(/^[\s,]+/,"") 
        .replace(/[\s,]+$/,"") 
        .replace(/\s*,+\s*(,+\s*)*/g,",") 

這人會opera, internet explorer, whatever

工作實際測試過這最後一個,和它的作品!當你想更換",," ",,,", ",,,,"",,,,,"下面的代碼會被","被刪除

+0

爲什麼downvote?我的正則表達式有什麼問題? – Victor 2009-11-03 09:26:29

+0

那麼,他們有點難看,是更好? – Victor 2009-11-03 09:56:12

+0

根本不工作! – 2009-11-03 10:00:30

0

匹配()是這個更好的工具,不是取代()

str = " aa, bb,, cc , dd,,,"; 
newStr = str.match(/[^\s,]+/g).join(",") 
alert("[" + newStr + "]") 
+0

這將刪除所有的空格,並用逗號替換 「,chrome,firefox,,, Internet Explorer,safari,opera ,,失敗 – 2009-11-03 09:48:29

0

var abc = new String("46590,26.91667,75.81667,,,45346,27.18333,78.01667,,,45630,12.97194,77.59369,,,47413,19.07283,72.88261,,,45981,13.08784,80.27847,,"); 
var pqr= abc.replace(/,,/g,',').replace(/,,/g, ','); 

alert(pqr); 
1

沒有這麼複雜,但簡單的搭配:

',google,, , yahoo,, ,'.replace(/\s/g, '').replace(/,+/g, ','); 
1

您應該能夠只使用一個替代電話:

/^(*, *)+|(, *(?=,|$))+/g 

測試:

'google, yahoo,, ,'.replace(/^(*, *)+|(, *(?=,|$))+/g, ''); 
"google, yahoo" 
',google,, , yahoo,, ,'.replace(/^(*, *)+|(, *(?=,|$))+/g, ''); 
"google, yahoo" 

細分:

/ 
    ^(*, *)+  # Match start of string followed by zero or more spaces 
       # followed by , followed by zero or more spaces. 
       # Repeat one or more times 
    |    # regex or 
    (, *(?=,|$))+ # Match , followed by zero or more spaces which have a comma 
       # after it or EOL. Repeat one or more times 
/g    # `g` modifier will run on until there is no more matches 

(?=...)是展望未來將不會移動比賽的位置,但只驗證一個字符是否匹配後。在我們的情況下,我們尋找,或EOL