Vishal Seth
Vishal Seth

Reputation: 5048

javascript regex for matching attributes in HTML string

Can anyone look at my regex in javascript and suggest a correct one?

I'm trying to select attributes(name/value) pairs in an HTML/XML string like following?

<unknowncustom:tag attrib1="XX' XX'" attrib2='YY" YY"' attrib3=ZZ""'>/unknowncustom:tag>

SOME TEXT that is not part of any tag and should not be selected, name='XX', y='ee';

<custom:tag attrib1="XX' XX'" attrib2='YY" YY"' attrib3=ZZ""'>/custom:tag>

I found many solutions but none seem foolproof (including this one Regular expression for extracting tag attributes)

My current regex selects the first attribute pair but can't figure out how to make it select all matching attributes. Here is the regex:

/<\w*:?\w*\s+(?:((\w*)\s*=\s*((?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))))[^>]*>/gim

Thanks

Upvotes: 0

Views: 2675

Answers (1)

Lee Kowalkowski
Lee Kowalkowski

Reputation: 11751

Let's have a go:

/(\w+)\s*=\s*((["'])(.*?)\3|([^>\s]*)(?=\s|\/>))(?=[^<]*>)/g

Regex is not ideal for this. If your attributes contain unescaped angle brackets < > it probably will not work.

Proof: http://regex101.com/r/dD4uT4

Upvotes: 3

Related Questions