正则表达式排除包含字符串模式的匹配

时间:2022-09-13 07:35:10

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.

我正在尝试缩小我的RegEx以忽略type =“submit”的表单元素。我只想选择元素部分直到part =“*”,但仍然忽略如果type =“submit”出现在类之前或之后。

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?

这是真的吗?

2 个解决方案

#1


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.

在正则表达式的开头添加(?!。* type =“submit。*)似乎给了我想要的结果。

Working Regex:

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

#2


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.

此表达式绑定到单个标记。最好避免像。*之类的表达式,因为它可以更进一步,匹配一个字符串,该字符串将在一个标记内开始,最终在另一个标记内。

#1


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.

在正则表达式的开头添加(?!。* type =“submit。*)似乎给了我想要的结果。

Working Regex:

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

#2


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.

此表达式绑定到单个标记。最好避免像。*之类的表达式,因为它可以更进一步,匹配一个字符串,该字符串将在一个标记内开始,最终在另一个标记内。