0

I've a large amount of encoded text, like this:

<div id="readingPaneContentContainer" class="ClearBoth"  cmp="cmp" ulr="ulr"><a id="rpFocusElt" href="javascript:void(0)&#59;" style="height:1px&#59;width:1px&#59

I would like de-encode all, so to have (Example):

<div id="readingPaneContentContainer" class="ClearBoth".....

Is possible to do with Regular Expressions?

Any help would be appreciated.

Luca

Luca
  • 335
  • 1
  • 4
  • 15

2 Answers2

1

See this thread - it has your solution for jQuery that works perfectly:

How to decode HTML entities using jQuery?

var encoded = '&#60;div id&#61;&#34;readingPaneContentContainer&#34; class&#61;&#34;ClearBoth&#34;  cmp&#61;&#34;cmp&#34; ulr&#61;&#34;ulr&#34;&#62;&#60;a id&#61;&#34;rpFocusElt&#34; href&#61;&#34;javascript&#58;void&#40;0&#41;&#59;&#34; style&#61;&#34;height&#58;1px&#59;width&#58;1px&#59';

var decoded = $("<div/>").html(encoded).text();

Does not use regex.

Community
  • 1
  • 1
methai
  • 8,835
  • 1
  • 22
  • 21
0

Something along the lines of:

var regex = /&#(\d{2});/g;
var match;
while(match = regex.exec(myString)) {
    match = match[1];
    myString = myString.substring(0, regex.lastIndex - 5) + convert[match] + myString.substring(regex.lastIndex);
}

might work but there must be better solutions (considering convert is an object that makes the conversion, eg 80: '<').

Explaination: regex.exec with a 'global' flag allows looping through the regex, regex.lastPoint's value being the pointer to the rest of the string (not being tested yet).

Edit:
Working.

Loamhoof
  • 8,293
  • 27
  • 30
  • Sorry parenthesis error in the regex, I changed it, maybe try again (I'll run some tests myself). – Loamhoof Mar 29 '13 at 16:17
  • Now i get the following error: convert is not defined Thanks you for the help ;) – Luca Mar 29 '13 at 19:15
  • Quote: *(considering convert is an object that makes the conversion, eg 80: '<')* You have to build this map before doing it. Anyway doing it with jQuery is a safer and easier way. – Loamhoof Mar 30 '13 at 10:51