Javascript ES6 使用 Regex 过滤数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47008384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ES6 filter an array with Regex
提问by Dileet
I'm trying to filter an array that contains a bunch of urls. I need to return the urls that only contain the word "contact".
我正在尝试过滤包含一堆 url 的数组。我需要返回只包含“联系”一词的网址。
For example there is a link https://www.example.com/v1/contact-us/ca
例如有一个链接 https://www.example.com/v1/contact-us/ca
This should be returned from the filter.
这应该从过滤器返回。
I tried this:
我试过这个:
const regex = new RegExp("/\bcontact\b", 'g' )
sites.links.filter((val) => {
console.log(regex.test(val.href))
})
It currently just sends back false through all the domains, when I know there is one domain that contains the word "contact".
它目前只是通过所有域发回 false,当我知道有一个域包含“联系人”一词时。
回答by Jaromanda X
Firstly new RegExp('/\bcontact\b', 'g');is equivalent to /\/@contact@/gwhere the @are backspace character (ASCII 08) ... clearly not what you want
首先new RegExp('/\bcontact\b', 'g');是等同于/\/@contact@/g其中@的退格字符(ASCII 08)......显然不是你想要什么
So, you would do new RegExp('/\\bcontact\\b', 'g');- this is equivalent to /\/\bcontact\b/g
所以,你会这样做new RegExp('/\\bcontact\\b', 'g');- 这相当于/\/\bcontact\b/g
However, the \\bafter /is redundant
然而,\\bafter/是多余的
so ... down to /\/contact\b/g
所以......归结为 /\/contact\b/g
Using string.matchhere as regex.testis misused. Below is the description
string.match在这里使用asregex.test被误用了。下面是说明
var sites = {
links: [
{href: 'https://www.example.com/v1/contact-us/ca'},
{href: 'https://www.example.com/v1/contact-us/au'},
{href: 'https://www.example.com/v1/contact-us/us'},
{href: 'https://www.example.com/v1/dontcontact-us/us'}
]
};
const regex = new RegExp('/contact\b', 'g');
const matchedSites = sites.links.filter(({href}) => href.match(regex));
console.log(matchedSites);
The next problem is using the ONE regex multiple times in a regexp.testwith gflag. With each call, it will look from the next indexOfprevious found substring and with consecutive calls on a same-type string, it basically will return true, false, true, false.
下一个问题是在regexp.testwithg标志中多次使用 ONE 正则表达式。每次调用时,它都会从下一个indexOf之前找到的子字符串中查找,并且对相同类型的字符串进行连续调用,它基本上会返回true, false, true, false。
If you want to use regex.test, then don't re-use the same regex unless you know the consequences of doing so or do not use gflag (which here you do not need)
如果您想使用regex.test,则不要重复使用相同的正则表达式,除非您知道这样做的后果或不使用g标志(在这里您不需要)
var sites = {
links: [
{href: 'https://www.example.com/v1/contact-us/ca'},
{href: 'https://www.example.com/v1/contact-us/au'},
{href: 'https://www.example.com/v1/contact-us/us'},
{href: 'https://www.example.com/v1/dontcontact-us/us'}
]
};
const regex = new RegExp('/contact\b', 'g');
const correctRegex = new RegExp('/contact\b');
const matchedSitesFailed = sites.links.filter(({href}) => regex.test(href));
const matchedSitesSuccess = sites.links.filter(({href}) => new RegExp('/contact\b', 'g').test(href));
const matchedSitesSuccess2 = sites.links.filter(({href}) => correctRegex.test(href));
console.log('failed returns:', matchedSitesFailed.length);
console.log('success returns:', matchedSitesSuccess.length);
console.log('success returns 2:', matchedSitesSuccess2.length);
回答by felixmosh
You need to return the truthy / falsy result from filterfunction.
您需要从filter函数返回真/假结果。
const regex = new RegExp("/\b?contact\b?", 'g');
const sites = {
links: [{
href: 'http://www.some-site.com/contact-us'
},
{
href: 'http://www.some-site.com/about'
},
{
href: 'http://www.some-site.com/admin'
}
]
}
const fitered = sites.links.filter((link) => {
return link.href.match(regex);
});
console.log(fitered);
回答by John Haugeland
I realize that the question requests regex, but the sensible answer here is
我意识到这个问题需要正则表达式,但这里的明智答案是
someArray.filter(str => str.includes('contact'))
回答by Aditya Parmar
var links = ["https://www.example.com/v1/contact-us/ca", "https://www.example.com/v1/contact-us/sanjose", "https://www.example.com/v1/meeting-us/ca"];
var newlink = links.filter(function(link){
return link.includes("contact")
});
console.log(newlink)
Try this. It should work.
尝试这个。它应该工作。

