简体   繁体   中英

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). With the following block of code, it removes everything, including the text inside the <p>

MyString.replace(/<p.*>/, '<p>');

Example: <p style="test" class="test">my content</p> gives <p></p>

Thank you in advance for your help!

Try this RegEx: /<p [^>]*>/ , basically just remove the closing bracket from the accepted characters. . 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 . 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

MyString.replace(/\<p.*<\/p>/, '<p></p>');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM