简体   繁体   English

用正则表达式过滤每个数组项

[英]filter each array item with regex

I have this regex that converts any # in a string to a hash tag array 我有这个正则表达式将字符串中的任何#转换为哈希标签数组

myArr= myStr.match(/#\S+/g);

I also have a this to filter out from a string anything other than alpah numeric minus underscore. 我也有一个this可以从字符串中过滤掉除alpah数字减下划线以外的任何内容。

myStr= myStr.replace(/[^a-zA-Z0-9_]/g, "");

But now i want to combine these 2 to make sure that each of the items in that array contain only alpha numeric values including underscore and the # in the begining. 但是现在我要结合这2个,以确保该数组中的每个项目仅包含字母数字值,包括下划线和开头的#

Can anyone help out please, thank you. 任何人都可以帮忙,谢谢。

Why so complicated? 为什么这么复杂?

myArr = myStr.match(/#[a-z0-9_]+/gi);

EDIT: I misunderstood your intention. 编辑:我误解了你的意图。 Here's a slightly less simple, but altogether more efficient, solution: 这是一个稍微简单一些但更有效的解决方案:

myArr = myStr.replace(/#\S+/g,function(m) {
    return "#"+m.replace(/[^a-z0-9_]+/gi,"");
}).match(/#\S+/g);

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

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