简体   繁体   中英

How to filter strings from HTML tags in javascript with regular expression

I have a solution that I could get string from HTML tags with regex as follow:

params.replace(/<[^>]*>/g, '');

Above code is when html tag found, it replace with ''. But what I want to get like array list ['ppshein', 'male', 'javascript'] if my original string is <b>ppshein</b><span>male</span><u>javascript</u>

Please let me know how to do it, thanks.

I used regex to remove tags and then split it then clear empty ones

 var string="<b>ppshein</b><span>male</span><u>javascript</u>"; var x=string.replace(/(<([^>]+)>)/ig, '[caps]').split("[caps]").filter(x=>x;=""). console;log(x);

Try this. Just split and remove the empty ones.

var string="<b>ppshein</b><span>male</span><u>javascript</u>";
var x=string.split(/<[^/].*?>(.*?)<\/.*?>/g).filter(x => (x != "")));
console.log(x);

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