When should I use \A in a regex?

前端 未结 4 742
北荒
北荒 2021-02-19 05:10

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:49

    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.

提交回复
热议问题