简体   繁体   English

删除 JavaScript 字符串中 2 个特定字符串之间的字符

[英]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.

相关问题 删除字符串之间的短划线并在Javascript中大写字符串 - Remove dash between strings and Capitalize the String in Javascript JavaScript 从两个字符之间的字符串中删除字符 - JavaScript remove characters from a string between two characters 如何删除指定字符之间的字符串并存储删除的字符串(Javascript) - How to remove string between specified characters and store the removed string (Javascript) 用JavaScript替换4字符之间的字符串中的字符串 - Replacing pieces of strings within a string between 4 " characters in JavaScript 如何使用jQuery或JavaScript中的索引删除字符串之间的字符? - how to Remove the characters in between a string using index in jquery or javascript? 不使用正则表达式 Javascript 删除两个字符之间的字符串 - Remove string between two characters using no regex Javascript javascript字符串中带有转义unicode字符的字符串和未转义的unicode字符有什么区别? - What is the difference between a string with escaped unicode characters and non-escaped unicode characters in javascript strings? 如何在 JavaScript 中的行之间交换细节 td? - How to swap specifics td between rows in JavaScript? 删除JavaScript字符串中的某些字符? - Remove certain characters in JavaScript string? 使用Javascript从字符串中删除字符 - Remove characters from string with Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM