简体   繁体   中英

Regular expression match string with wildcard

I have a string with the following format: 'val1/val2/val3/val4'. I want a function which when comparing this string format with this string value 'val1/?/val3/?' to return true. The '?' can be any value. I have tried with the string include method but this doesn't cover the '?' value.How can I do this using regular expressions in 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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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