0

I am trying to grab everything inside a tr using javascript expressions.

This is where i am at right now:

var data = /<tr bgcolor="#FFFFFF">([\s\S])/.exec(request.responseText);

I have found on google that [\s\S] means all characters and new lines, i hope this is correct.

request.responseText is where i get my data from.

This is the value it returns:

"<tr bgcolor="#FFFFFF">", ""

I am using regExr.com to make these expressions. I have searched around on google but the only awnsers i find are in normal javascript.

So to recap my question, i want to get the data between the <tr> that has bgcolor="#FFFFFF" in it until the following </tr> into a variable so i can use it in a array that i will be making.

From here  --> <tr bgcolor="#FFFFFF">
                <td>random text</td>
                <td>random text</td>
                <td>random text</td>
                <td>random text</td>
              </tr> <-- until here

I hope my question is clear, its hard explaining something you dont know much about. First time using Javascript expressions.

Edit: This is going to be a chrome plugin, and has already been made using regex. So changing it is way out of my skill level. I got given this project as an intern.

So document.getelementbyID for example is not what i want. I have already used this in the past.

Granny
  • 783
  • 11
  • 22
  • 1
    [you can't parse HTML using Regex](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – Liam Aug 29 '17 at 11:57
  • `/([\s\S]+)<\/tr>/` ? – Timothé Malahieude Aug 29 '17 at 11:59
  • @TimothéMalahieude This is close but it doesnt stop at the it just keeps going. – Granny Aug 29 '17 at 12:00
  • Possible duplicate of [Getting HTML elements by their attribute names](https://stackoverflow.com/questions/7647095/getting-html-elements-by-their-attribute-names) – Liam Aug 29 '17 at 12:00
  • @Liam Im not asking how to get the HTML elements by their attribute name, its not even close. – Granny Aug 29 '17 at 12:01
  • Apart form the drawbacks to querying HTML using regex it's much better to use the inbuilt selectors: `document.querySelectorAll("tr[bgcolor='#FFFFFF']");` – Liam Aug 29 '17 at 12:01
  • @Liam I totally agree its a better way, but the project i got given has already been developed using these expressions so i cant just change it. – Granny Aug 29 '17 at 12:05
  • Sorry but expected output is not very clear. Can you write the real expected output? – Fefux Aug 29 '17 at 12:05
  • Why? Parsing HTML using regex is not the right way to do this? It will never work well. – Liam Aug 29 '17 at 12:07
  • @Liam I started my internship yesterday and this is the first project i got given, i couldn't tell you why we are using this. Could it be because its for a chrome plugin? Cause its a chrome plugin not a website or anything. – Granny Aug 29 '17 at 12:08
  • 1
    Well the person who originally wrote this is wrong. Regex parsing HTML is difficult to maintain, inaccurate and there are better options available (yes for plugins too). You can continue this incorrect behaviour or you can do it the right way (see duplicate) and then maybe say, hey guys look, I've found a better way to do this. If it's a good company they'll welcome your input and congratulate you. If they don't well, I'd look for a better internship. – Liam Aug 29 '17 at 12:11
  • Maybe this can help you `([\s\S]+?)<\/tr>`. But if you can stop parsing HTML with Regex, do it – Fefux Aug 29 '17 at 12:23
  • 2
    @Liam I talked to the guy, i am now gonna use the document. tags. – Granny Aug 29 '17 at 13:02
  • Good work, this is the right way to go – Liam Aug 29 '17 at 13:05
  • 2
    H̸̡̪̯ͨ͊̽̅̾̎Ȩ̬̩̾͛ͪ̈́̀́͘ ̶̧̨̱̹̭̯ͧ̾ͬC̷̙̲̝͖ͭ̏ͥͮ͟Oͮ͏̮̪̝͍M̲̖͊̒ͪͩͬ̚̚͜Ȇ̴̟̟͙̞ͩ͌͝S̨̥̫͎̭ͯ̿̔̀ͅ – EJoshuaS - Stand with Ukraine Aug 29 '17 at 15:27
  • Possible duplicate of [RegEx match open tags except XHTML self-contained tags](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – EJoshuaS - Stand with Ukraine Aug 29 '17 at 15:27

1 Answers1

1

I think this should work for your case:

/<tr bgcolor="#FFFFFF"(.|\n)*?\/tr>/

(.|\n)*? will match any characters or line breaks in the tr block.

hoan
  • 1,058
  • 8
  • 13