Reputation: 329
Original version:
<div>
<p>
<span>text</span>
</p>
<div>
RegExp:
$.get('/some.html', function(data) {
alert(data.replace(/<p.*p>/gi, ''));
});
After RegExp:
<div>
<>
<span>text</span>
</>
<div>
What do I need to get:
<div>
<div>
Upvotes: 1
Views: 4692
Reputation: 11028
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(' ');
Upvotes: 0
Reputation: 7723
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, ''));
Upvotes: 0
Reputation: 92986
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.
Upvotes: 2
Reputation: 53396
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');
Upvotes: 2