When should I use \A in a regex?

前端 未结 4 794
暗喜
暗喜 2021-02-19 05:32

End of line anchor $ match even there is extra trailing \\n in matched string, so we use \\Z instead of $

For example

4条回答
  •  暖寄归人
    2021-02-19 05:41

    Not directly relevant to your question according to the tags you used, but there is at least one language (Ruby) where ^ and $ always mean start/end-of-line, so if you want to match start/end-of-string you have to use \A and \Z or \z.

    If you want to keep your regexes portable, it's good practice to explicitly state what you want them to do instead of relying on the availability of mode modifiers like \m or Regex.MULTILINE etc.

    On the other hand, JavaScript, POSIX and XML do not support \A and \Z. This is where tools like RegexBuddy come in handy that translate regexes from one flavor to the other for you.

提交回复
热议问题