检查javascrpit中给定字符串数组的字符串匹配 [英] check string matches from the given array of string in javascrpit

查看:89
本文介绍了检查javascrpit中给定字符串数组的字符串匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的字符串与给定的字符串数组匹配,但是下面的代码不起作用,是否有任何建议可能有帮助,下面的代码就是我试过的

i trying to match my string with given array of string but this below code is not working,is any suggestion might be help , below code is what i have tried

let myLongString = 'jkjdssfhhabf.pdf&awersds=oerefsf';
let matcherArray = ['.pdf', '.jpg'];

if (myLongString.match(matcherArray)) {
    return true;
} else {
    return false;
}

预期输出 true 。是否有更好的解决方案可以解决这类问题谢谢。

expected output is true. is there is any better solution is there to solve this type question thank you.

推荐答案

无需循环

let myLongString = 'jkjdssfhhabf.pdf&awersds=oerefsf';
console.log(/\.(jpe?g|pdf)/i.test(myLongString));

这篇关于检查javascrpit中给定字符串数组的字符串匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆