How do I recognize strings that do not end with a

2020-04-11 07:49发布

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 how can I test to see if it doesn't?

标签: regex
3条回答
够拽才男人
2楼-- · 2020-04-11 08:30

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:

/(?<!\/)$/
查看更多
来,给爷笑一个
3楼-- · 2020-04-11 08:51

[^\/]$

^ will negate any character class expression.

查看更多
男人必须洒脱
4楼-- · 2020-04-11 08:56

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

查看更多
登录 后发表回答