Original version:
<div>
<p>
<span>text</span>
</p>
<div>
RegExp:
$.get('/some.html', function(data) {
alert(data.replace(/<p.*p>/gi, ''));
});
After RegExp:
<div>
&l开发者_运维技巧t;>
<span>text</span>
</>
<div>
What do I need to get:
<div>
<div>
Javascript has no dotall mode so your .
does not match newline characters.
but you can try this
$.get('/some.html', function(data) {
alert(data.replace(/<p[\S\s]*?p>/gi, ''));
});
[\S\s]
means match any non whitespace (\S
) or any whitespace (\s
) character. the newline characters are included in the whitespace characters.
The *?
is a non greedy match, means it matches as less as possible.
As soon as your tags are nested, you will run into problems when using regular expressions. You have to be aware of that. Probably the solution from @fearofawhackplanet is the better choice.
If you want to work with the returned html, then you shouldn't be doing that through regex.
I'm not completely sure I understand your question, but it looks like you are trying to remove the <p>
elements from the result? You can work with the returned html with jQuery, something like
$(data).remove('p');
Javascript regexp don't match \n with ".". And you specified //g flag. in normaly, use [\s\S] in javascript.
alert(data.replace(/<p[\s\S]*?p>/i, ''));
if i got you correctly then you need something like this...but it will make all div in your page as a blank div
$('div').html('');
if you want remove specific div then give an id to that div and then do it like this..
$(data).find('#divID').html(' ');
精彩评论