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?
dsgriffin
68.8k17 gold badges141 silver badges139 bronze badges
asked May 21, 2013 at 13:19
3 Answers 3
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
answered May 21, 2013 at 13:48
3 Comments
bphillips
If you are using jQuery, this is pretty nifty. Pretty creative way to parse html entites and just return tag value. Very nice. Got my upvote
user2405589
Hey, thanks for your reply... but I guess I was wronged into entering an already escaped string. The original string should read as '<a href="somesite/…^&wicket:pageMapName=wicket-2">SomeThing</a>'; I don't think your script replaces string with escaped characters.
Edper
Hello @user2405589 my code could decode escape characters. But you need to edit your original post so that it would be clearer then we could help you more. Like what you are giving me now is not also clear. So, kindly edit your post make more clearer and understandable. Provide ample example or scenario as much as possible. GOD bless.
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 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);
answered May 21, 2013 at 13:32
Comments
Here is a possible starting point.
Hope this gets you started!
function parseString(){
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 begin = str.indexOf('\">',0)+2; //--determine where the opening anchor tag ends
var end = str.indexOf('</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('</a>',0));
*/
console.log(parsedString);
}
parseStr();
answered May 21, 2013 at 13:37
Comments
lang-js
<
is the entity name, not the escape code.