简体   繁体   English

正则表达式将几个 URL 模式与 Javascript 匹配

[英]RegEx to match several URL patterns with Javascript

I'm trying to match any of several URL patterns in Javascript.我正在尝试匹配 Javascript 中的几个 URL 模式中的任何一个。 The patterns are:模式是:

  • The home page - the / without anything after.主页/之后没有任何内容。
  • One of three solutions pages.三个解决方案页面之一。 Each solutions(number) could be followed by a / and any characters after.每个solutions(number)后面可以跟一个/和后面的任何字符。
    • /solutions/99043 or /solutions/99043/blah /solutions/99043/solutions/99043/blah
    • /solutions/60009 or /solutions/60009/blah /solutions/60009/solutions/60009/blah
    • /solutions/40117 or /solutions/40117/blah /solutions/40117/solutions/40117/blah
  • Search: /search followed by any characters after, eg ?blah .搜索: /search后跟任何字符,例如?blah

The RegEx I tried is as follows:我试过的正则表达式如下:

/\/$|\/solutions\/(99043|60009|40117)\/.*|\/search.*/

In this function:在此 function 中:

(function () {
    const urlPath = window.location.pathname;
    if (urlPath.match(/\/$|\/solutions\/(99043|60009|40117)\/.*|\/search.*/)) {
        console.log("urlPath", urlPath);
    }
})()

It doesn't work in that everything seems to be matched.它不起作用,因为一切似乎都匹配。 Anyone have any ideas where I went wrong?有人知道我哪里出错了吗?

Based on a comment, an example of a URL that matches but shouldn't: /solutions/根据评论,一个匹配但不应该匹配的 URL 示例: /solutions/

If you are extracting the pathname from URL and then performing the matching, I would recommend to use ^\/$ instead of just matching “ends with slash”.如果您从 URL 中提取路径名然后执行匹配,我建议使用^\/$而不是仅匹配“以斜线结尾”。

So that would be ^\/$|\/solutions\/(99043|60009|40117)\/.*|\/search.*所以这将是^\/$|\/solutions\/(99043|60009|40117)\/.*|\/search.*

You can test it on regex101.com.您可以在 regex101.com 上对其进行测试。 I've found regulex to be really helpful for visualizing regular expressions.我发现正则表达式对于可视化正则表达式非常有帮助。

You could of anchors to assert that start ^ and the end $ of the string.您可以使用锚来断言字符串的开始^和结束$

Match / and optionally match either the part with solutions followed by the 3 numbers or match the search part using an alternation .匹配/并且可选地匹配具有解后跟 3 个数字的部分或使用交替匹配搜索部分。

^\/(?:solutions\/(?:99043|60009|40117)(?:\/.*)?|search\b.*)?$
  • ^ Start of string ^字符串开头
  • \/ Match / \/匹配/
  • (?: Non capturing group (?:非捕获组
    • solutions\/ Match solutions/ solutions\/匹配solutions/
    • (?:99043|60009|40117) Match 1 of the 3 numbers (?:99043|60009|40117)匹配 3 个数字中的 1 个
    • (?:\/.*)? Optionally match / and any char except a newline 0+ times可选择匹配/和除换行符以外的任何字符 0+ 次
    • | Or或者
    • search\b.* Match search followed by a word boundary to not match for example searchhere search\b.*匹配搜索后跟不匹配的单词边界,例如searchhere
  • )? Close non capturing group and make it optional关闭非捕获组并使其可选
  • $ End of string $字符串结尾

Regex demo正则表达式演示

You can use the following regular expression:您可以使用以下正则表达式:

^\/((solutions(\/(99043|60009|40117)(\/.*)?)?)|search(.*)?)$

Test:测试:

 var regex = /^\/((solutions(\/(99043|60009|40117)?(\/.*)?)?)|search(.*)?)?$/ console.log(1, regex.test('/')) // true console.log(2, regex.test('/solutions')) // true console.log(3, regex.test('/solutions/')) // true console.log(4, regex.test('/solutions/99043')) // true console.log(5, regex.test('/solutions/99043/')) // true console.log(6, regex.test('/solutions/99043/anything')) // true console.log(7, regex.test('/solutions/60009')) // true console.log(8, regex.test('/solutions/60009/')) // true console.log(9, regex.test('/solutions/60009/anything')) // true console.log(10, regex.test('/solutions/40117')) // true console.log(11, regex.test('/solutions/40117/')) // true console.log(12, regex.test('/solutions/40117/anything')) // true console.log(13, regex.test('/solutions/00000')) // false console.log(14, regex.test('/solutions/00000/')) // false console.log(15, regex.test('/solutions/00000/anything')) // false console.log(16, regex.test('/bug')) // false console.log(17, regex.test('/search?query=javascript')) // true console.log(18, regex.test('/search/?query=javascript')) // true

So, this regular expression prevents from the following bugs:因此,此正则表达式可防止以下错误:

  • Prevents testing sub-string rather then a full pathname:防止测试子字符串而不是完整路径名:

/bug/solutions/99043 // false /bug/solutions/99043 // 错误

  • Prevents testing just a part of solutions numbers:防止仅测试解决方案编号的一部分:

/solutions/990430000 // false /solutions/990430000 // 假

/solutions/000099043 // false /solutions/000099043 // 假

\/(solutions|search)(\/(99043|60009|40117).*|)

https://regex101.com/r/nqtB4v/2 https://regex101.com/r/nqtB4v/2

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

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