1

I'm trying to narrow down my RegEx to ignore form elements with type="submit". I only want to select the portion of elements up to the part class="*" but still ignore if type="submit" comes before or after the class.

My regular expression thus far:

(<(?:input|select|textarea){1}.*[^type="submit"]class=")(((?!form\-control)[a-zA-Z0-9_ -])*")

Test case: Line one should match up to the end of class, and line 2 ignored.

<input type="text" name="name" id="test" class="example-class" max-length="7" required="required">
<input type="submit" class="btn-primary" value="send">

Is this acheivable?

Kiee
  • 10,661
  • 8
  • 31
  • 56

2 Answers2

0

Thanks for your comments. The answer was a negative look ahead.

Adding (?!.*type="submit.*) to the start of the regex appears to have given me my desired result.

Working Regex:

(?!.*type="submit.*)(<(?:input|select|textarea).*class=")(((?!form\-control)[a-zA-Z0-9_ -])*")
Kiee
  • 10,661
  • 8
  • 31
  • 56
  • We still think a program that uses a valid HTML parser would make more sense – OneCricketeer Feb 22 '17 at 12:36
  • I'm not sure why a HTML parser keeps getting referenced. I want to perform a project wide search and replace of multiple HTML files using a regular expression pattern. Is a HTML parser still relevant for this scenario? – Kiee Feb 22 '17 at 12:38
  • Absolutely. You can write a small script in any language to search over many files in a folder. It just depends on your needs. – OneCricketeer Feb 22 '17 at 12:42
0
(<(?:input|select|textarea)\s((?!type="submit")[\w\-]+\b="[^"]*"\s?)*>)

This expression is bound to the single tag.
It is better to avoid expressions like .* since it can go further and match a string which would begin inside one tag and end-up inside another.

Pavel Agarkov
  • 3,633
  • 22
  • 18