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

7
  • 1
    What language are you using? Commented Mar 29, 2013 at 15:44
  • jQuery, but i think that will don't change anything..correct? Commented Mar 29, 2013 at 15:45
  • 1
    regex implementation is different in different languages. Also it makes a difference to how one repesents the entites in unicode. Commented Mar 29, 2013 at 15:48
  • Possibly of interest - RegEx match open tags except XHTML self-contained tags Commented Mar 29, 2013 at 15:49
  • @DanPichelman : This regex wouldn't replace encoded tags but match only existent (Not encoded). Commented Mar 29, 2013 at 15:53

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Works fine, but to maintain the tag must be removed .text() Thanks you
This is really slow compared to regexp
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.

3 Comments

Sorry parenthesis error in the regex, I changed it, maybe try again (I'll run some tests myself).
Now i get the following error: convert is not defined Thanks you for the help ;)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.