user2405589
user2405589

Reputation: 968

Escape characters in String in a HTML page?

I have a string in the below non-escaped format in a HTML page:

<a href="http://somesite/product?page=blahk&id=EA393216&tabs=7,0&selections=quarter:Q2+2013^&wicket:pageMapName=wicket-2\">SomeThing</a>

What I need is to use jQuery/JavaScript to replace that string with just the link "SomeThing".

I have looked at some examples in StackOverflow, but they don't seem to work. I'm just getting started with jQuery and JavaScript, so would appreciate any help here.

Any ideas?

Upvotes: 0

Views: 162

Answers (3)

Edper
Edper

Reputation: 9322

Try html() and text() in jquery to decode:

var str = '<a href="http://somesite/product?page=blahk&id=EA393216&tabs=7,0&selections=quarter:Q2+2013^&wicket:pageMapName=wicket-2\">SomeThing</a>';


 var decoded = $('<div />').html(str).text();

 alert($(decoded).text());

See Fiddle demo

Upvotes: 1

bphillips
bphillips

Reputation: 459

Here is a possible starting point.

Hope this gets you started!

function parseString(){
    var str = '&lt;a href="http://somesite/product?page=blahk&id=EA393216&tabs=7,0&selections=quarter:Q2+2013^&wicket:pageMapName=wicket-2\">SomeThing&lt;/a>';
    var begin = str.indexOf('\">',0)+2;             //--determine where the opening anchor tag ends  
    var end = str.indexOf('&lt;/a>',0);             //--determine where the closing anchor tag begins
    var parsedString = str.substring(begin,end);    //--grab whats in between;
    /*//--or all inline
    var parsedString = str.substring(str.indexOf('\">',0)+2,str.indexOf('&lt;/a>',0));
    */
    console.log(parsedString);
}
parseStr();

Upvotes: 0

deceze
deceze

Reputation: 522626

var str = '&lt;a href="http://somesite/product?page=blahk&id=EA393216&tabs=7,0&selections=quarter:Q2+2013^&wicket:pageMapName=wicket-2\">SomeThing&lt;/a>';
var helper = document.createElement('p');

// evaluate as HTML once, text now "<a href..."
helper.innerHtml = str;

// evaluate as HTML again, helper now has child element a
helper.innerHtml = helper.innerText;

// get text content only ("SomeThing")
alert(helper.innerText);

Upvotes: 0

Related Questions