Regex - Match attribute in a HTML code [duplicate]

+7 votes
asked Oct 6, 2011 by tony

This question already has an answer here:

5 Answers

+4 votes
answered Oct 6, 2011 by john-keyes

The * is a greedy quantifier. You should follow it with a question mark to make it non-greedy:

myAttr=\"([^']*?)\"
+15 votes
answered Oct 6, 2011 by ray-toal

You have an apostrophe (') inside your character class but you wanted a quote (").

myAttr=\"([^"]*)\"

That said, you really shouldn't be parsing HTML with regexes. (Sorry to link to that answer again. There are other answers to that question that are more of the "if you know what you are doing..." variety. But it is good to be aware of.)

Note that even if you limit your regexing to just attributes you have a lot to consider:

  • Be careful not to match inside of comments.
  • Be careful not to match inside of CDATA sections.
  • What if attributes are bracketed with single quotes instead of double quotes?
  • What if attributes have no quotes at all?

This is why pre-built, serious parsers are generally called for.

+1 vote
answered Oct 6, 2011 by laurent

If you only want the myAttr parameter value, use this:

"myAttr=\"([^\"]+)\""
0 votes
answered Oct 6, 2011 by merianos-nikos

you can try use that

 myAttr=\"?[\w:\-]+ ?= ?("[^"]+"|'[^']+'|\w+)\"
0 votes
answered Mar 7, 2017 by user7671441

<[^>]*>

Just try this is this help for remove all tag

Example Something

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...