How can I replace numeric entities in a string?

Ask questions about how to create a script or swap scripts with other users.
Post Reply
User avatar
pjj
Posts: 1784
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

How can I replace numeric entities in a string?

Post by pjj »

I'm cleaning up a document, which has plenty of "&x160;" (=non-breaking space) entities; I want to replace them with regular spaces. When I try

Code: Select all

Document.ReplaceAll(" ", " ", false, false, false);
search results panel opens up, telling me that "0 items found in 0 of 1 searched files" (sic!). Searching for a "regular" string I get correct message ("in 1 of 1 searched files".)
When I try

Code: Select all

Document.SelectAll;
s = Document.SelText;
s = ScriptUtils.StringReplaceAll(s, " ", " ");
Document.SelText = s;
nothing is replaced, either.

My guess is that this &x160; should be escaped somehow, but how exactly? When I search for &x160 instead (no closing semicolon) with StringReplaceAll(), I get a message
"160" is not a valid integer value
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus

User avatar
micha_he
Posts: 529
Joined: 24 Jul 2011 12:16
Location: Helmstedt, NDS, Germany

Re: How can I replace numeric entities in a string?

Post by micha_he »

Maybe...

Edit - New try:

Code: Select all

s = ScriptUtils.RegExReplaceAll(s, "\xA0", " ");

User avatar
pjj
Posts: 1784
Joined: 13 Oct 2009 13:48
Location: Kraków, Poland

Re: How can I replace numeric entities in a string?

Post by pjj »

Vielen Dank, aber es funktioniert nicht :(

Does it work for you? I have literal strings   (6 chars), not just single entities with code no. 160.

OK, I can replace it in two steps:

Code: Select all

s = ScriptUtils.StringReplaceAll(s, "&#", "");
s = ScriptUtils.StringReplaceAll(s, "160;", " ");
but to be on a safe side I should probably use something like this:

Code: Select all

s = ScriptUtils.StringReplaceAll(s, "&#", "$$$");
s = ScriptUtils.StringReplaceAll(s, "$$$160;", " ");
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus

User avatar
micha_he
Posts: 529
Joined: 24 Jul 2011 12:16
Location: Helmstedt, NDS, Germany

Re: How can I replace numeric entities in a string?

Post by micha_he »

Same here ! Maybe an error...
But you can use:

Code: Select all

s = ScriptUtils.RegExReplaceAll(s, "&(#)160;", " ");

User avatar
Rickard Johansson
Site Admin
Posts: 6001
Joined: 19 Jul 2006 14:29

Re: How can I replace numeric entities in a string?

Post by Rickard Johansson »

It's a bug. I've fixed it in the next release.

Post Reply