enchance
enchance

Reputation: 30421

Regular expression for selecting attributes name only from within certain tags

What's the regex which allows me to select all the attribute names from <form> and <input> tags but not from any other HTML tag?

For example:

<!-- all attribute names get selected -->
<input class="something" id="yes" type="text" name="my-field" value="Hello, world!">

<!-- class and id don't get selected because it's a div -->
<div class="something" id="no"></div>


<!-- class gets selected -->
<form class="my-form"></form>

I'm only after the attribute names

Upvotes: 0

Views: 186

Answers (2)

Ωmega
Ωmega

Reputation: 43673

It is not easy task to do it with regex and actually it is not a good idea to do it with regex. But it is possible >>

input = '...';

var tmp = input, found, params = [];
var re = /(<(?:form|input)\b.*?\s)([\w\-]+)=(['"]).*?\3/gi;
do {
  found = 0;
  tmp = tmp.replace(re, function($0,$1,$2,$3) {
    params.push($2);
    found = 1;
    return $1;
  });
} while (found);

Check this demo.

Upvotes: 0

Bergi
Bergi

Reputation: 664385

Such a regexp would be very complicated to build. Despite the fact that you can't match all HTML by regexes, it would need a very complicated lookbehind to check whether the attribute name which you want to match comes after a opening tag whose name is either "form" or "input". Don't try to build such a regex, you'd go crazy and/or end up with an unreadable, non-maintainable or -undestandable monster.

Instead, use a DOM parser (there will be one for your language) and apply DOM selectors and get the attribute names of the elements.

Upvotes: 1

Related Questions