2
4

2 回答 2

3

You need to use '\u2029' as the search string. The sequence you are trying to replace is a "paragraph separator" Unicode character inserted by InDesign.

So:

string.replace('\u2029', '');

instead of the character itself.

于 2015-11-25T17:28:24.567 回答
3

String.replace() doesn't work exactly the way you think. The way you use it, it'll only replace the first occurrence:

> "abc abc abc".replace("a", "x");
'xbc abc abc'

You need to add the g (global) flag and the only standard way is to use regular expression as match:

> "abc abc abc".replace(/a/g, "x");
'xbc xbc xbc'

You can have a look at Fastest method to replace all instances of a character in a string for further ideas.


A search for 0x80 0xE2 0xA9 as UTF-8 shows the character doesn't exist but it's probably a mistype for 0xE2 0x80 0xA9 which corresponds to 'PARAGRAPH SEPARATOR' (U+2029) as Goran points out in his answer. You don't normally need to encode exotic characters as JavaScript \u#### reference as long as all your tool-set is properly configured to use UTF-8 but, in this case, the JavaScript engine considers it a line feed and triggers a syntax error because you aren't allowed to have line feeds in JavaScript strings.

于 2015-11-25T17:42:23.143 回答