Reputation: 722
My String is given below ,
var str = "The test <del> SString </del> string , The other <del>textt </del> test string, <br/> show something";
I can write regex pattern which is removed only html tag , pattern is given below,
pattern = /(<([^>]+)>)/ig
In Str Variable, there can be any html tag like as bold tag(b), italic tag(i).... etc.
How to write regex pattern in jquery or javascript which will be remove inner text of html tag with html tag.
Actually the result will be,
"The test string , the other test string , show something"
Upvotes: 0
Views: 1759
Reputation: 46841
Make it Lazy
<([^>]+)>.*?<\/\1>|<.*?\/>
Here is demo
sample code:
var re = /<([^>]+)>.*?<\/\1>|<.*?\/>/g;
var str = 'The test <del> SString </del> string , The other <del>textt </del> test string, <br/> show $nbsp; something\n\nThe test string , the other test string , show something';
var result = str.replace(re, '');
EDIT
Note: The above regex does not support tags having attributes.
Try below regex to cover any attributes having in tag. Here is the DEMO
<([^( |>)]+)([^>]*)>.*?<\/\1>|<.*?\/>
Upvotes: 2
Reputation: 2047
var str = "The test <del> SString </del> string , The other <del>textt </del> test string, <br/> show $nbsp; something";
What you want
$("<div>"+str+"</div>").text()
//result retun is: "The test SString string , The other textt test string, show $nbsp; something"
Upvotes: 0
Reputation: 24312
Following Function will help you,
function getChildText(node) {
var text = "";
for (var child = node.firstChild; !! child; child = child.nextSibling) {
if (child.nodeType === 3) {
text += child.nodeValue;
}
}
return text;
}
Upvotes: 1
Reputation: 1121
This would work.
var str = "The test <del> SString </del> string , The other <del>textt </del> test string, <br/> show $nbsp; something";
$a = $('<div>'+str+'</div>');
$a.children().remove()
var whatYouWant = $a.text();
Upvotes: 1