开发者

Crop first and last tag with javascript IF it's the same tag

开发者 https://www.devze.com 2023-02-03 22:58 出处:网络
I\'m tr开发者_JAVA百科ying to delete tags from string: \"<p>string</p>\" must be converted to \"string\"

I'm tr开发者_JAVA百科ying to delete tags from string:

"<p>string</p>" must be converted to "string"

I found solution for simply cropping first and last tags or all tags and then

"<p>string1</p><p>string2</p>" converts to "string1</p><p>string2"

I want to check string, something like

"IF last </p> tag is closing tag for first <p> tag THEN crop"

Is this possible to do with regex? If no, I think formula must be something like: <p> tags equals 1, </p> equals -1. I start to sum this numbers from end of string and stop when sum equals zero. If point where I stop is the end of string then it's one tag and I need crop it. Otherwise it's different tags. But I don't know how to code it, help please.


I know @drachenstern has posted that comment pretty much answering your question but still. You can use a fairly simple regex along side the replace() function. This little function should do the trick.

function stripTags(str) {
    return str.replace(/<[^<]+?>/g, '');
}


I think I made this task too complicated. I forgot that <p> tag can't be in another <p>. And then I can simply do check

if ($("#my_div>p").length == 1) {
     // crop surrounding <p>
}

Thanks for your answers and sorry about stupid question, I just need more sleeping.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号