0

how can I separate a HTML table tag from string by using regular expression?

var sTabString =' ... <table > <table ... any string ... id="Unique_1"  ... any string ...> abc def <table > ... ';
var sReg = '< *table .* id *= *"Unique_1" .*>';
var sRegEx = new RegExp(sReg);
var sResult = sTabString .match(sRegEx);
alert(sResult);

I expect the open tag with all its attributes as follows:

<table ... any string ... id="Unique_1"  ... any string ...>
8
  • 4
    What is the XY problem?. Why do you need to do this? Commented Apr 7, 2014 at 10:11
  • I agree with @h2ooooooo - I wanted to link XY problem as well. Tell us what are you trying to achieve, because we are 98% sure you're not going to need regex for your task. Commented Apr 7, 2014 at 10:13
  • @ElmoVanKielmo I changed it to that as it was confusing. As far as I can understand given the string <table id="foo" class="table-bordered"><thead><tr><th>Hi</th></tr></thead></table> he wants <table id="foo" class="table-bordered"> (the open tag). Commented Apr 7, 2014 at 10:16
  • can you post the full table code and tell us what you want to be extracted ? Commented Apr 7, 2014 at 10:17
  • I'm trying to read certain HTML tables from text. Commented Apr 7, 2014 at 10:18

1 Answer 1

1

This will get you every <table> element (and all its attributes) in a string.

var regex = new RegExp("<table.*?>", "g");

var result = str.match(regex);  

for (var i=0; i<result.length; i++)
{
    // do something with your <table> elements here
    console.log(result[i]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.