正则表达式在属性中找到一个单词

时间:2022-10-05 03:40:06

I have a case like this:

我有这样的案例:

<p class="definitione **form**">Data una grandezza indica la variazione.</p><br/>
<p class="**form**">Due casi molto comuni sono:</p><br/>
<ul><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<li class="**form** placeholder">è il valore iniziale della grandezza.</li><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<li class="definition **form** placeholder">è il valore di in un secondo punto.</li><br/>
</ul><br/>
<p id="form">Se la grandezza aumenta, la variazione è positiva</p><br/><br/>

I need to find (and replace) only form word (and not the other word) into attribute class.

我需要找到(并替换)只有形式的单词(而不是另一个单词)到属性类。

Try with this regex:

试试这个正则表达式:

(?<=class=")form(?=")

select only:

<p class="definitione form">Data una grandezza indica la variazione.</p><br/>
<p class="**form**">Due casi molto comuni sono:</p><br/>
<ul><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<li class="form placeholder">è il valore iniziale della grandezza.</li><br/>
&nbsp;&nbsp;&nbsp;&nbsp;<li class="definition form placeholder">è il valore di in un secondo punto.</li><br/>
</ul><br/>
<p id="form">Se la grandezza aumenta, la variazione è positiva</p><br/><br/>

But I need to select ALL form in all class attribute

但我需要在所有class属性中选择ALL表单

1 个解决方案

#1


1  

To match classes with form in them:

要匹配带有表单的类:

(class="[^"]*)(form)([^"]*")

See a live demo of this regex, which uses look arounds to assert what precedes and follows the target.

查看此正则表达式的现场演示,该演示使用环视来断言目标之前和之后的内容。

Note that group 2 matches "form", not the whole expression. This is because variable length look behinds are illegal. Your replacement should refer to group 1 and 3 to put them back.

请注意,组2匹配“表单”,而不是整个表达式。这是因为可变长度的外观是非法的。您的更换应参考第1组和第3组将它们放回原处。

#1


1  

To match classes with form in them:

要匹配带有表单的类:

(class="[^"]*)(form)([^"]*")

See a live demo of this regex, which uses look arounds to assert what precedes and follows the target.

查看此正则表达式的现场演示,该演示使用环视来断言目标之前和之后的内容。

Note that group 2 matches "form", not the whole expression. This is because variable length look behinds are illegal. Your replacement should refer to group 1 and 3 to put them back.

请注意,组2匹配“表单”,而不是整个表达式。这是因为可变长度的外观是非法的。您的更换应参考第1组和第3组将它们放回原处。