簡體   English   中英

javascript正則表達式刪除空白失敗,為什么?

[英]javascript regex to remove whitespace fails, why?

我使用text.replace(/\\s/g, '')從字符串中刪除所有空白字符。

我正在俄語文字上嘗試這個。 我執行了一個alert(text) ,向我顯示了正確的字符串,但是replace函數拋出此錯誤- Bad Argument /\\s/g

我正在為Adobe InDesign腳本創建.jsx文件。 replace方法適用於某些字符串,但有時會失敗。 知道為什么嗎?

謝謝。

編輯

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--;
    }
.
.
.
}

我的錯-這是我修改過的答案。

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

alert(newStr) // "Hellothisismyteststring";
  • 如果文本中包含$,^等正則表達式特殊字符,則需要轉義(“ \\\\”)。

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

我正在使用.text()填充文本對象。 我了解到此函數將空間轉換為不間斷空間(字符160)。

也必須剝離...
text.replace(/&nbsp;|\\s+/g)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM