0

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?

3
  • I have a feeling that it might help to know how you get this string. If this is just what you end up with after an 'innerHTML' retrieval, there may be a better way to retrieve the value you need. (If this is, for some reason, returned flat from an AJAX call or something, then the problem is as you say it) Commented May 21, 2013 at 13:27
  • 1
    Your question statement reallly dsnt match with your actual query.. Commented May 21, 2013 at 13:32
  • 1
    @user2405589 < is the entity name, not the escape code. Commented May 21, 2013 at 13:48

3 Answers 3

1

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

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

3 Comments

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
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.
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.
0
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);

Comments

0

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();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.