Skip to content
Advertisement

Regex match HTML attributes names

I have the following element stored as a String:

JavaScript

I want to extract all the attributes names like this:

JavaScript

This is what I tried to do, but I get also the values and dosent match the data-animation and disabled:

http://jsbin.com/hibebezibo/edit?js,console

EDIT:

Manged to get attributes using:

JavaScript

But I still cant get the “disabled” prop.

Can someone explain me how to achieve this? Thanks!

Advertisement

Answer

Using below regex which benefits from a positive lookahead you are able to match attributes’ names:

JavaScript

Note: Adding - to character class is a must.

javascript code:

JavaScript

However it’s not bulletproof as it can match words following a >. E.g:

JavaScript

So it’s recommended to accomplish such a task using DOM functionalities:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement