简体   繁体   English

正则表达式匹配字符串与通配符

[英]Regular expression match string with wildcard

I have a string with the following format: 'val1/val2/val3/val4'. 我有一个字符串格式如下:'val1 / val2 / val3 / val4'。 I want a function which when comparing this string format with this string value 'val1/?/val3/?' 我想要一个函数,在将此字符串格式与此字符串值'val1 /?/ val3 /?'进行比较时 to return true. 返回真实。 The '?' '?' can be any value. 可以是任何价值。 I have tried with the string include method but this doesn't cover the '?' 我尝试过使用字符串include方法但这不包括'?' value.How can I do this using regular expressions in javascript? 如何在javascript中使用正则表达式来做到这一点?

Thanks in advance 提前致谢

Try using this pattern: 尝试使用此模式:

val1/[^/]+/val3/.*

Sample: 样品:

String input = "val1/some_path/val3/stuff";
if (input.matches("val1/[^/]+/val3/.*")) {
    System.out.println("match");
}

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

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