I have a string trackingObj
which stores a collection of information.(HTML codes, but there are many characters like \r\n
inside the codes, for example:
<div id=\"MainBox\">\r\n
I'd like to get following content from that giant string:
<td id='theTrackInfo'><strong><span id='HeaderNum'>aaa</span><span id='HeaderFrom'> <br>bbb</span><span id='HeaderDes'> <br>ccc</span><span id='HeaderItem'> <br>ddd</span><span id='HeaderState'> <br>eee</span><span id='HeaderADate'><br>fff</span><span id='HeaderSign'><br>ggg</span><DIV id='HeaderExtra'> </DIV></strong></td>
I tried to append the whole string to DOM using html() but there are illegal characters inside it, so I couldn't use jQuery to perform DOM manipulation.
just thinking about using pure regex to get what I need. I tried following:
var Info = new RegExp("<td>\sid='theTrackInfo'>[\s\S]*?\/td>", "g");
var InfoHtml = theTrackInfo.exec(trackingObj);
console.log(InfoHtml);
I also tried:
var InfoHtml = trackingObj.match(/<td>\sid='theTrackInfo'>[\s\S]*?<\/td>/gi);
console.log(InfoHtml);
doesn't work. What I am missing?
=================UPDATE==========================
Hi everyone, thank you for all of your answers.
I tried using DOMParser to make it work finally:
var parser = new DOMParser();
var html = parser.parseFromString(ProcessedStrings,"text/html");
var info = $(html).find("#theTrackInfo");
console.log($(info).html());
Some one may say jQuery should do same thing.
the problem is that, the trackingObj
is retrieved by using ajax call and when I try to use jQuery append method it append to DOM, in console, it said: "Unexpected token ILLEGAL"
But I will still choose a regex answer as correct answer for this question.
==================update 2============
Hi, I examined Tom Fenech's approach, it works for me too. probably the error is caused by trying to append the codes to a div. nothing to do with jQuery itself.