简体   繁体   中英

extract word using regular expression in javascript

Here is my string

var str = 'varying<div class="desc_1"><div>Changing or xyz</div></div>';

And I want to extract "varying" and "Changing or xyz" word from sentence using regular expression in javascript. I want output like this

str1 = "varying";
str2 = "Changing or xyz";


Js Fiddle Link

Do you need to use a regular expression here?

 var str = 'varying<div class="desc_1"><div>Changing or xyz</div></div>'; var tmp = document.createElement('div'); tmp.innerHTML = '<div>' + str + '</div>'; var text = []; var divs = tmp.getElementsByTagName('div'); for (var i = 0; i < divs.length; i++) { var textEle = divs[i].childNodes[0]; if (textEle.nodeValue !== null) { text.push(textEle.nodeValue); } } console.log(text); document.getElementById('output').innerHTML = text.join(', '); 
 <p id="output"></p> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM