0

I need to fing all "table" tags on page with attribute border on the page using Regular Expressions on JavaScript.

How can I do it?

P.S. Please, don't propose me any JQuery (or similar) solutions.

10
  • 2
    Have you taken a look at .querySelector? You can use CSS selectors to match what you want, without regex. Commented Jul 31, 2015 at 10:53
  • yes you can use regex like this. <table.*?border.*?>.*?</table> Commented Jul 31, 2015 at 10:56
  • Why can't you use native JS methods? How do you expect to do anything without being able to use native JS? Also, I don't think the querySelector is DOM manipulation. Commented Jul 31, 2015 at 10:56
  • 1
    @StasKh It's a trick. Tell your mentor that you should use the appropriate tool for the task. Commented Jul 31, 2015 at 10:58
  • 1
    @Xufox Thanks. I happen to agree with the second answer there. And will continue to post this link where appropriate. Commented Jul 31, 2015 at 11:01

2 Answers 2

3

Use this code

 document.querySelectorAll("table[border]")
Sign up to request clarification or add additional context in comments.

2 Comments

PLease explain your answer in detail
If your answer is short and link-only you might get negative points. So make it informative so other user also get what you answered.
-1

you can use regex object like this to match table which has border.

myTableElement.match(/<table.*?border.*?>.*?<\/table>/gi)

or like this

var x =  new RegExp(/<table.*?border.*?>.*?<\/table>/gi)

myTableElement.match(x);

5 Comments

Sorry, how can I select all table tags with any attributes except border attribute?
you can use new RegExp(/<table\s+border=['"].*?['"]\s+>.*<\/table>/)
Although I'm not the down voter, the reason is probably that you're a enabler. You're giving OP the answer where he is approaching the problem with wrong idea. There is no reason, ever, why you would have to use regex to parse html. Especially with powerful javascript api in browsers, like document.querySelectorAll. If this is a homework, stack overflow isn't the right place and whoever gave the homework should read about regexes and how they are not meant for parsing html.
then why it has been downvote i just given some idea
stackoverflow is for giving some information to the questions

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.