简体   繁体   English

JavaScript以部分匹配字符串

[英]JavaScript for partial match in a string

I am trying to match if a string contains any of the following values and I was wondering if there is a way to do it in JS with out indexOf or includes with multiple || 我正在尝试匹配一个字符串是否包含以下任何值,并且我想知道是否有一种方法可以在JS中使用indexOf或包含多个||来实现​​。 ?

Eg: 例如:

    const referrer = "www.google.com/login/auth";
    const loginRoute = "/login/auth";
    const logoutRoute = "/logout/";
    if(referrer.includes(loginRoute) || referrer.includes(logoutRoute)) {
       //do something
    }

is there any other way of doing this as I am trying to avoid multiple || 还有其他方法可以这样做,因为我试图避免多个|| conditions? 条件? in my implementation I have to check with five different URIs. 在我的实现中,我必须检查五个不同的URI。

您可以使用正则表达式:

if (/\/login\/auth|\/logout\//.test(referrer))

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

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