0

I have this piece of HTML code:

<tr class="AttCardFooter" onMouseOver="this.style.backgroundColor='#E1EAFE'" onMouseOut="this.style.backgroundColor='Transparent'">...etc etc.. new lines etc.. <td title="Saldo+">33:33</td><td title="Saldo-">22:22</td> .. etc etc... 

I need JavaScript regex that will grab these two fields 33:33 and 22:22 All that I have tried fails because of new line characters. If anyone knows how to accomplish that I would be very grateful.

ajax333221
  • 11,436
  • 16
  • 61
  • 95
Marko3d
  • 21
  • 1
  • 3
  • It's generally considered a bad idea to use regex to parse HTML. I'd recommend you use something like xpath instead. https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript – Mike Tangolics Apr 02 '12 at 19:06
  • There needs to be a count of how many JavaScript regex questions are about parsing HTML. – maerics Apr 02 '12 at 19:18

4 Answers4

2

Don't use a regexp. I guess your piece of html is an innerHTML or outerHTML of some element. Instead of parsing html with regexp do this:

var el = document.querySelector("tr.AttCardFooter"); // I guess you have that variable already
for (var i=0; i<el.cells.length; i++) {
     var td = el.cells[i];
     if (td.title == "Saldo+")
         var positiveSaldo = td.innerText;
     else if (td.title == "Saldo-")
         var negativeSaldo = td.innerText;
}
alert("Voila:\n"+positiveSaldo+"\n"+negativeSaldo);

You may improve this with your favorite libraries dom functions. For example in jQuery it would be something like

 var el = $("tr.AttCardFooter");
 var positiveSaldo = el.find('td[title="Saldo+"]').text();
 var negativeSaldo = el.find('td[title="Saldo-"]').text();
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

You would have to process the new line characters. Try this:

Pattern p = Pattern.compile(myRegExp, Pattern.DOT_ALL);
Andrés Oviedo
  • 1,388
  • 1
  • 13
  • 28
0

Try this lib and add /s flag so the dot matches new lines.

http://xregexp.com/

Daniel Bang
  • 715
  • 6
  • 21
0

Regular expressions cannot parse HTML.

If the table row is already part of your document, then there's no need to do anything fancy. Use the DOM to extract the values you need -- Bergi has great suggestions.

If the string comes from somewhere else, consider using a hidden <div> and set its innerHTML. The browser handles parsing the string; you then use DOM methods to extract the values you need.

Of course, if that "somewhere else" is untrusted input (third party site, user input, etc), you can't just use a hidden <div> due to the security problems of blindly sticking potentially executable code into your page. Instead, you need to sandbox this step in an <iframe> running on a different Origin.

Community
  • 1
  • 1
josh3736
  • 139,160
  • 33
  • 216
  • 263