Pyae Phyoe Shein
Pyae Phyoe Shein

Reputation: 13837

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.

Upvotes: 0

Views: 1785

Answers (2)

Jack Ting
Jack Ting

Reputation: 571

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);

Upvotes: 0

Cagri Tacyildiz
Cagri Tacyildiz

Reputation: 17610

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);

Upvotes: 1

Related Questions