How do I recognize strings that do not end with a slash character ('/') using a regex?

前端 未结 3 1469
忘了有多久
忘了有多久 2021-01-20 21:09

How can i match a string that does not finish with / . I know I can do that /\\/$/ and it will match if string does finish with /, but

相关标签:
3条回答
  • 2021-01-20 21:38

    You can say "not character" by doing [^...]. In this case, you can say "not backslash by doing": /[^\/]$/

    0 讨论(0)
  • 2021-01-20 21:40

    You can use a negative character class:

    /[^\/]$/
    

    This however requires that the string contains at least one character. If you also want to allow the empty string you can use an alternation:

    /[^\/]$|^$/
    

    A different approach is to use a negative lookbehind but note that many popular regular expression engines do not support lookbehinds:

    /(?<!\/)$/
    
    0 讨论(0)
  • 2021-01-20 21:46

    [^\/]$

    ^ will negate any character class expression.

    0 讨论(0)
提交回复
热议问题