31

I have problem with matching the html attributes (in a various html tags) with regex. To do so, I use the pattern:

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

HTML snippet:

<img alt="" src="1-p2.jpg" myAttr="http://example.com" class="alignleft" />

it selects text from the myAttr the end /> but I need to select the myAttr="..." ("http://example.com")

0

5 Answers 5

40

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.

2
  • if the attribute value contain \" ,this regex will be wrong such as <a href="dddd" c="\"abc\""/>
    – xiaoyifang
    Apr 16, 2022 at 7:18
  • Nice, another good reason not to use regexes here! It is completely the wrong approach.
    – Ray Toal
    Apr 17, 2022 at 20:13
10

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

myAttr=\"([^']*?)\"
3

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

"myAttr=\"([^\"]+)\""
2

you can try use that

 myAttr=\"?[\w:\-]+ ?= ?("[^"]+"|'[^']+'|\w+)\"
-5

<[^>]*>

Just try this is this help for remove all tag

Example Something

1
  • 5
    Did you read the question?
    – Toto
    Mar 7, 2017 at 9:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.