-1

I have an regex and i need to extract data from document.body.outerHTML i tried various methods like .search() , .match() & .exec() but it didn't work i need to extract data from whole document.body.outerHTML. My class is as below.

class executionPrompt {
   constructor(){
       this.regex = /(B[0-9]{2})/
       this.data = [];
   }
   start(){
       var html  = jQuery(document.body.outerHTML);
       var matches = jQuery(html).html();
       console.log(matches.match(this.regex));
   }
}

i have whole document and regex but i am not able to get array for my matching string. Please help me.

Nasiruddin Saiyed
  • 1,438
  • 1
  • 13
  • 31
  • 1
    What data are you trying to access? Parsing HTML through Regex is [a really bad idea](https://stackoverflow.com/a/1732454/519413). Use a proper HTML parser instead. – Rory McCrossan May 09 '18 at 09:41
  • Why the jQuery? Just match against `document.body.outerHTML` or use proper parsing methods instead – CertainPerformance May 09 '18 at 09:42
  • 1
    Just FYI: to match all occurrences, a `g` modifier is required, `/B[0-9]{2}/g`. – Wiktor Stribiżew May 09 '18 at 09:42
  • https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 It is cause why you shouldn't do it. – Kornelia Kobiela May 09 '18 at 09:45
  • @RoryMcCrossan its because i have to bind click event on id passing my regex (which i can't post full) i just have snippets, and actual HTML will be generated by my server on requirement. – Nasiruddin Saiyed May 09 '18 at 09:52
  • i have to parse each nodes occuring my body so that it generate its tree its very complicated to wait till server response so i need to parse my outerHTML – Nasiruddin Saiyed May 09 '18 at 09:54

1 Answers1

-1

From this answer You can't parse [X]HTML with regex. Because HTML can't be parsed by regex. Regex is not a tool that can be used to correctly parse HTML. As I have answered in HTML-and-regex questions here so many times before, the use of regex will not allow you to consume HTML. Regular expressions are a tool that is insufficiently sophisticated to understand the constructs employed by HTML. HTML is not a regular language and hence cannot be parsed by regular expressions. Regex queries are not equipped to break down HTML into its meaningful parts. ...

i as commented by @Wiktor Stribiżew i have change my mind and as of now i have requested to server to grabs id then get its index from tree,

FYI: to match all occurrences, a g modifier is required, /B[0-9]{2}/g may be seems an acceptable answer thumbs up for all.

Nasiruddin Saiyed
  • 1,438
  • 1
  • 13
  • 31