Javascript ES6使用正则表达式过滤数组
我正在尝试筛选包含一组URL的数组。我需要返回的网址,只包含单词“联系” 例如,有一个链接Javascript ES6使用正则表达式过滤数组,javascript,node.js,regex,Javascript,Node.js,Regex,我正在尝试筛选包含一组URL的数组。我需要返回的网址,只包含单词“联系” 例如,有一个链接https://www.example.com/v1/contact-us/ca 这应该从过滤器返回 我试过这个: const regex = new RegExp("/\bcontact\b", 'g' ) sites.links.filter((val) => { console.log(regex.test(val.href)) }) 当我知道有一个域包含
https://www.example.com/v1/contact-us/ca
这应该从过滤器返回
我试过这个:
const regex = new RegExp("/\bcontact\b", 'g' )
sites.links.filter((val) => {
console.log(regex.test(val.href))
})
当我知道有一个域包含单词“contact”时,它当前只是通过所有域发回false。您需要从
filter
函数返回truthy/falsy结果
const regex=new RegExp(“/\b?contact\b?”,“g”);
常数站点={
链接:[{
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)=>{
返回link.href.match(regex);
});
控制台日志(已安装)代码>
试试这个。它应该可以工作。首先新的RegExp('/\bcontact\b',g')
相当于/\/@contact@/g
,其中@
是退格字符(ASCII 08)。。。显然不是你想要的
因此,您需要执行newregexp('/\\bcontact\\b','g')代码>-这相当于/\/\b联系人\b/g
但是,/
之后的\\b
是多余的
所以。。。下至/\/contact\b/g
在此处使用string.match
,因为regex.test
被误用。下面是描述
var站点={
链接:[
{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));
控制台日志(匹配站点);
下一个问题是在带有g
标志的regexp.test
中多次使用一个正则表达式。每次调用时,它都会从上一个找到的子字符串的下一个索引中查找,如果连续调用同一类型的字符串,它基本上会返回true
,false
,true
,false
如果要使用regex.test
,则不要重复使用同一个regex,除非您知道这样做的后果或不使用g
标志(此处不需要)
var站点={
链接:[
{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 matchedssitessuccess=sites.links.filter({href})=>newregexp('/contact\\b','g').test(href));
const matchedssitessuccess2=sites.links.filter(({href})=>correctRegex.test(href));
log('failed returns:',matchedSitesFailed.length);
log('success returns:',matchedssitessuccess.length);
log('success返回2:',matchedssitessuccess2.length)代码>我知道这个问题需要正则表达式,但这里明智的答案是
someArray.filter(str => str.includes('contact'))
您需要在筛选器中返回truthy/false。。。不返回任何内容===返回未定义==返回false。。。另外,无论如何,您都将丢弃筛选的结果,因此您的代码主要是pointless@JaromandaX你这个愚蠢的拼写错误。更新。那么如果我做regex.test(val.href)?return val.href:nullconst matchedSites=sites.links.filter(val=>regex.test(val.href))代码>-注意/\bconsole\b
第一个\b
是多余的-您需要新的RegExp(“/contact\\b”,“g”)
。。。soconst regex=new RegExp('/contact\\b',g')
@JaromandaX我用这个返回了两个空数组,所以如果我这样做了
-不,请先阅读以了解如何使用过滤器。。。您可能还需要一个关于regexp的复习,更新了使用regex.no@Dileet的答案-这是因为代码正确地使用了filter。。。顺便说一下,这个代码仍然可以fail@felixmosh-检查-您的代码无法返回两个联系人页面,因为您正在使用regex.test,但不了解它。为此,非常感谢您。你刚刚给了我一些知识,朋友。我也扩展了答案。。。请小心使用regex.test:p@JaromandaX谢谢你的回答。然而,我有一个奇怪的问题,当我使用regex.test进行过滤时,我遗漏了一些与条件匹配的数据。原因是什么?实际上,使用regex.match代替regex.test解决了这个问题。你能解释一下吗?谢谢。你的意思是包括吗?这对我来说是最好的答案!非常直截了当@阿卡迪乌斯兹兰齐安-是的。感谢您解释您的答案,这将使您的答案更容易被接受并吸引更多的听众,而此代码可能会回答问题,提供有关如何和/或为什么解决问题的附加上下文,从而提高答案的长期价值。
someArray.filter(str => str.includes('contact'))
const regex = new RegExp('/contact\\b', 'g');
const matchedSites = sites.links.filter(e =>
regex.test(e.href));
console.log(matchedSites);