2010-05-18 41 views
2

請任何人都可以將此Python代碼轉換爲javascript。如何將此Python標點剝離功能轉換爲JavaScript?

# 
def strip_punctuation(s): 
# 
    for c in ',.":;!%$': 
# 
     while s.find(c) is not -1: 
# 
      s.replace(c, '') 
+8

通過你的腳本不執行任何操作的方式。 s.replace的結果被丟棄。 s最終沒有變化。 strip_punctuation()返回None。 – 2010-05-18 22:30:04

+0

嗡嗡聲。好點子。我想我的大腦只是填滿了空白。 – 2010-05-18 23:01:08

+1

點是從字符串中去除標點符號。現在我需要從字符串中刪除符號和數字。 – 2010-05-19 07:02:55

回答

12
function strip_punctuation(s) { 
    return s.replace(/[,.":;!%$]/g, ""); 
} 
+0

謝謝。你能告訴我如何去掉字符串中的符號和數字。我找不到那個正則表達式 – 2010-05-19 07:01:19

+4

@Ayaz:那麼,不要去尋找一個,那麼學習如何在http://www.regular-expressions.info/上自己寫呢? – 2010-05-19 09:25:48