代码之家  ›  专栏  ›  技术社区  ›  Catfish

用于匹配任何主机的url的正则表达式

  •  1
  • Catfish  · 技术社区  · 5 年前

    /profile/documents/<uuid>

    下面的正则表达式匹配 /配置文件/文档/<uuid> 但是如何修改这个正则表达式以匹配 任何东西 /配置文件/文档/<uuid> ?

    expect(res.headers.location).to.match(/^\/profile\/documents\/[A-F\d]{8}-[A-F\d]{4}-4[A-F\d]{3}-[89AB][A-F\d]{3}-[A-F\d]{12}$/i) // Regex for /profile/documents/:uuid
    

    我希望匹配的示例有:

    http://localhost:3000/profile/documents/8ce825b7-b8c2-468b-a4c8-88eedea5b4c2
    http://127.0.0.1/49943/profile/documents/8ce825b7-b8c2-468b-a4c8-88eedea5b4c2
    
    2 回复  |  直到 5 年前
        1
  •  3
  •   Jack Bashford    5 年前

    简单-只需删除 ^ 一开始。(这与字符串的开头匹配,因此只有 /profile/document/<uuid> 应该是匹配的-删除它匹配任何字符串 在里面 /配置文件/文档/<uuid> ):

    /\/profile\/documents\/[A-F\d]{8}-[A-F\d]{4}-4[A-F\d]{3}-[89AB][A-F\d]{3}-[A-F\d]{12}$/i
    
        2
  •  0
  •   Blackjack    5 年前

    你可以试试这个 .*\/profile\/documents\/[a-f\d]{8}-[A-f\d]{4}-4[A-f\d]{3}-[89ab][a-f\d]{3}-[a-f\d]{12}$

    你可以看看这里的例子 regex-example