regular express : how to exclude multiple character groups?

前端 未结 3 2079
既然无缘
既然无缘 2021-02-07 18:47

I have a set of urls :

/products

/categories

/customers

Now say a customers is named john, and I want to help john to reac

3条回答
  •  你的背包
    2021-02-07 18:55

    What you need here is a negative lookahead assertion. What you want to say is "I want to match any string of characters, except for these particular strings." An assertion in a regex can match against a string, but it doesn't consume any characters, allowing those character to be matched by the rest of your regex. You can specify a negative assertion by wrapping a pattern in (?! and ).

    '/^\/(?!products|categories|admin)(.+)$/'
    

    Note that you might want the following instead, if you don't allow customers names to include slashes:

    '/^\/(?!products|categories|admin)([^/]+)$/'
    

提交回复
热议问题