views:

70

answers:

2

given the following string:

var htmlStr = '<p class="red_349dsa01">This is</p><p class="blue_saf9vsaz">a test</p>';

how can I remove the very first and last tag off this string? This would be the result:

var htmlStr = 'This is</p><p class="blue_saf9vsaz">a test';

I know this will create invalid HTML, but I just want to know if this can be done at all.

A: 

You would need a regular expression here:

var regex = /(?:^<p[^>]*>)|(?:<\/p>$)/g;
var htmlStr = '<p class="red_349dsa01">This is</p><p class="blue_saf9vsaz">a test</p>';  
htmlStr.replace(regex, "");

An explanation of the regex:

  1. The first part (?:^<p[^>]*>) uses the caret ^ character to match the start of the string,
  2. then <p will match the start of the opening p tag,
  3. [^>]* will match any character except the > character,
  4. the | splits the expression into two, one in each pair of braces where either can be matched,
  5. the <\/p>$ expression will match a closing </p> tag only if it is right at the end of the string by using the $ character.
Andy E
A: 

You could try something like

var htmlStr = '<p class="red_349dsa01">This is</p><p class="blue_saf9vsaz">a test</p>';
alert(htmlStr.substring(htmlStr.indexOf('>') + 1, htmlStr.lastIndexOf('<')));
astander
I always like to see a non-regex option, but after all the splitting, reversing, rejoining and string searching, would this be any faster?
Andy E
It was the only reverse implementation I found. Many languages has built in string reversing, and i find that the easiest way to find a last reference, unless the language implemets indexOf with negative searching.
astander
Found it, funny enoug, it was **lastIndexOf**. Sorry, that seems a lot easier.
astander
lol yeah. It didn't occur to me you could just swap it all for lastIndexOf, I must be getting slow. I would +1 but I've run out of votes for today.
Andy E