2013-05-30 60 views
0

我使用text.replace(/\s/g, '')刪除字符串中的所有空白字符。JavaScript正則表達式刪除空格失敗,爲什麼?

我想在俄羅斯文本上。我做了一個alert(text),它向我顯示了正確的字符串,但替換函數會引發此錯誤 - Bad Argument /\s/g

我正在爲Adobe InDesign腳本創建.jsx文件。替換方法適用於某些字符串,但有時會失敗。任何想法爲什麼?

謝謝。

編輯

for (var i=0; i<arr.length; i++) { 
    // If there is no text for the current entry, remove it 
    alert(arr[i].text); 
    if (arr[i].text == undefined || arr[i].text === "") { 
     arr.splice(i,1); 
     i--; 
     continue; 
    } 


    var trimmed = arr[i].text.replace(/\s/g, ''); 
     if (trimmed.text === "") { 
     entries.splice(i,1); 
     i--; 
    } 
. 
. 
. 
} 
+2

一個正則表達式刪除空白......只是,_why?_ –

+0

InDesign的API返回空白的段落,@GrantThomas。我只是檢查段落是否包含沒有有意義的文本,我不會輸出它。所以,我試圖刪除所有空白,並與「」進行比較。 – divyanshm

+0

你可以發表小提琴嗎? – elclanrs

回答

0

我的壞 - 這是我的編輯答案。

var str = "Hello this is my test string"; 
var newStr = str.replace(/ /g, ''); 

alert(newStr) // "Hellothisismyteststring"; 
+0

http://stackoverflow.com/questions/6163169/removing-whitespace-from-string-in-javascript – divyanshm

0
  • 你需要逃避( 「\\」),如果有喜歡的任何$正則表達式的特殊字符,^,等等......你文字

- 試着發佈小提琴或粘貼失敗的文本,我們可以檢查問題。

0

我正在使用.text()來填充文本對象。我瞭解到這個函數將空間轉換爲非破壞空間(字符160)。

只好脫光太...
text.replace(/&nbsp;|\s+/g)