0

I'm trying 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.

ink
  • 649
  • 1
  • 9
  • 22
  • Hi @Ink and welcome to the site. Sadly what you're asking is one of our most popular questions. http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags ... sadly it is the nature of this site to close duplicate questions that can really be answered by other more mature questions replete with answers, but the best answer you're going to get is "use a library". In this case, I would recommmend jQuery. It'll do what you want in many ways for instance http://api.jquery.com/unwrap/ – jcolebrand Jan 12 '11 at 02:52
  • Thanks, I'll try to make some check and replacement function with jQuery. – ink Jan 12 '11 at 12:09

2 Answers2

0

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, '');
}
Olical
  • 39,703
  • 12
  • 54
  • 77
  • I dont need to strip all tags, only one and only if first and last tags are opening and closing parts of one tag. – ink Jan 12 '11 at 12:08
0

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.

ink
  • 649
  • 1
  • 9
  • 22