When should I use \A in a regex?

前端 未结 4 817
暗喜
暗喜 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

    If the regex flavor you're working with supports \A then I recommend you always use it instead of ^. \A always matches at the start of the string only in all flavors that support it. There is no issue with line breaks.

    ^ may match at the start of the string only or at the start of any line depending on the regex flavor and regex options.

    By using \A you reduce the potential for confusion when somebody else has to maintain your code.

提交回复
热议问题