简体   繁体   English

如何使用正则表达式从 javascript 中的 HTML 标记中过滤字符串

[英]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:我有一个解决方案,我可以使用正则表达式从 HTML 标签获取字符串,如下所示:

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

Above code is when html tag found, it replace with ''.上面的代码是当 html 标签找到时,它用''替换。 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>但是如果我的原始字符串是 <b> ['ppshein', 'male', 'javascript'] <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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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