[英]Remove characters between 2 specifics strings in a JavaScript string
I'm trying to remove all the characters between the characters <p
and </p>
(basically all the attributes in the p
tags).我正在尝试删除字符<p
和</p>
之间的所有字符(基本上是p
标签中的所有属性)。 With the following block of code, it removes everything, including the text inside the <p>
使用以下代码块,它会删除所有内容,包括<p>
中的文本
MyString.replace(/<p.*>/, '<p>');
Example: <p style="test" class="test">my content</p>
gives <p></p>
示例: <p style="test" class="test">my content</p>
给出<p></p>
Thank you in advance for your help!预先感谢您的帮助!
Try this RegEx: /<p [^>]*>/
, basically just remove the closing bracket from the accepted characters.试试这个正则表达式: /<p [^>]*>/
,基本上只是从接受的字符中删除右括号。 .
matches all characters, that's why this doesn't work.匹配所有字符,这就是为什么这不起作用。 With the new one it stops at the first >
.有了新的,它停在第一个>
。
Edit: You can add a global and multi-line flag: /<p [^>]*>/gm
.编辑:您可以添加一个全局和多行标志: /<p [^>]*>/gm
。 Also as one of the comments pointed out, removing the tag makes it applicant for every tag, however this will make replacing a bit harder.同样正如其中一条评论所指出的那样,删除标签使其适用于每个标签,但这会使替换变得更加困难。 This RegEx is: /<[^>]*>/gm
这个正则表达式是: /<[^>]*>/gm
MyString.replace(/\<p.*<\/p>/, '<p></p>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.