Ori Refael
Ori Refael

Reputation: 3018

Javascript delete div text from string

I have a string which contains 2 div in it with their text and settings. for example :

<div class="a" id="b">blabla </div><div class="a">Here is the text i need to get </div>

I need to pull out the text from this string, cant use sub string cause the text is dynamic and not always written the same. Thanks

Upvotes: 0

Views: 112

Answers (2)

xdazz
xdazz

Reputation: 160833

var str = '<div class="a" id="b">blabla </div><div class="a">Here is the text i need to get </div>';
var tmp = document.createElement('div');
tmp.innerHTML = str;
console.log(tmp.innerText || tmp.textContent);  // is this what you want?

Upvotes: 3

Arun P Johny
Arun P Johny

Reputation: 388326

Try a regex replace like

'<div class="a" id="b">blabla </div><div class="a">Here is the text i need to get </div>'.replace(/(\<div.*?\>|\<\/div\>)/g, '')

Demo: Fiddle

Upvotes: 1

Related Questions