I have some HTML and the requirement is to remove only starting <p>
tags from the string.
我有一些HTML,并且要求只从字符串中删除起始
标记。
Example:
例:
input: <p style="display:inline; margin: 40pt;"><span style="font:XXXX;"> Text1 Here</span></p><p style="margin: 50pt"><span style="font:XXXX">Text2 Here</span></p> <p style="display:inline; margin: 40pt;"><span style="font:XXXX;"> Text3 Here</span></p>the string goes on like that
desired output: <span style="font:XXXX;"> Text1 Here</span></p><span style="font:XXXX">Text2 Here</span></p><span style="font:XXXX;"> Text3 Here</span></p>
Is it possible using Regex? I have tried some combinations but not working. This is all a single string. Any advice appreciated.
是否可以使用正则表达式?我尝试了一些组合,但没有工作。这只是一个字符串。任何建议表示赞赏
2 个解决方案
#1
15
I'm sure you know the warnings about using regex to match html. With these disclaimers, you can do this:
我确定你知道关于使用正则表达式来匹配html的警告。有了这些免责声明,您可以这样做:
Option 1: Leaving the closing </p>
tags
选项1:保留结束 标签
This first option leaves the closing </p>
tags, but that's what your desired output shows. :) Option 2 will remove them as well.
第一个选项留下结束 标签,但这就是您所需的输出显示的内容。 :)选项2也将删除它们。
PHP
PHP
$replaced = preg_replace('~<p[^>]*>~', '', $yourstring);
JavaScript
JavaScript的
replaced = yourstring.replace(/<p[^>]*>/g, "");
Python
蟒蛇
replaced = re.sub("<p[^>]*>", "", yourstring)
-
<p
matches the beginning of the tag - 匹配标记的开头
- The negative character class
[^>]*
matches any character that is not a closing>
- 否定字符类[^>] *匹配任何不是结束的字符>
-
>
closes the match - >结束比赛
- we replace all this with an empty string
- 我们用空字符串替换所有这些
Option 2: Also removing the closing </p>
tags
选项2:同时删除结束 标签
PHP
PHP
$replaced = preg_replace('~</?p[^>]*>~', '', $yourstring);
JavaScript
JavaScript的
replaced = yourstring.replace(/<\/?p[^>]*>/g, "");
Python
蟒蛇
replaced = re.sub("</?p[^>]*>", "", yourstring)
#2
0
This is a PCRE expression:
这是一个PCRE表达式:
/<p( *\w+=("[^"]*"|'[^']'|[^ >]))*>(.*<\/p>)/Ug
Replace each occurrence with $3 or just remove all occurrences of:
用$ 3替换每个匹配项,或者只删除所有出现的:
/<p( *\w+=("[^"]*"|'[^']'|[^ >]))*>/g
If you want to remove the closing tag as well:
如果您还想删除结束标记:
/<p( *\w+=("[^"]*"|'[^']'|[^ >]))*>(.*)<\/p>/Ug
#1
15
I'm sure you know the warnings about using regex to match html. With these disclaimers, you can do this:
我确定你知道关于使用正则表达式来匹配html的警告。有了这些免责声明,您可以这样做:
Option 1: Leaving the closing </p>
tags
选项1:保留结束 标签
This first option leaves the closing </p>
tags, but that's what your desired output shows. :) Option 2 will remove them as well.
第一个选项留下结束 标签,但这就是您所需的输出显示的内容。 :)选项2也将删除它们。
PHP
PHP
$replaced = preg_replace('~<p[^>]*>~', '', $yourstring);
JavaScript
JavaScript的
replaced = yourstring.replace(/<p[^>]*>/g, "");
Python
蟒蛇
replaced = re.sub("<p[^>]*>", "", yourstring)
-
<p
matches the beginning of the tag - 匹配标记的开头
- The negative character class
[^>]*
matches any character that is not a closing>
- 否定字符类[^>] *匹配任何不是结束的字符>
-
>
closes the match - >结束比赛
- we replace all this with an empty string
- 我们用空字符串替换所有这些
Option 2: Also removing the closing </p>
tags
选项2:同时删除结束 标签
PHP
PHP
$replaced = preg_replace('~</?p[^>]*>~', '', $yourstring);
JavaScript
JavaScript的
replaced = yourstring.replace(/<\/?p[^>]*>/g, "");
Python
蟒蛇
replaced = re.sub("</?p[^>]*>", "", yourstring)
#2
0
This is a PCRE expression:
这是一个PCRE表达式:
/<p( *\w+=("[^"]*"|'[^']'|[^ >]))*>(.*<\/p>)/Ug
Replace each occurrence with $3 or just remove all occurrences of:
用$ 3替换每个匹配项,或者只删除所有出现的:
/<p( *\w+=("[^"]*"|'[^']'|[^ >]))*>/g
If you want to remove the closing tag as well:
如果您还想删除结束标记:
/<p( *\w+=("[^"]*"|'[^']'|[^ >]))*>(.*)<\/p>/Ug