2013-05-30 63 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)