How do you match a caret (^) symbol in regex?

前端 未结 2 891
野趣味
野趣味 2020-11-30 15:10

I know these won\'t do it and also why, but how do you match it?

/^/
/(^)/
/[^]/
相关标签:
2条回答
  • 2020-11-30 15:41

    I think this works (I've tested this in java):

    \\^
    

    '\' is used as an escape character, so you first escape '^', then you escape '\' itself

    you can find more information here: http://www.regular-expressions.info/characters.html

    0 讨论(0)
  • 2020-11-30 15:48

    Escape it with a backslash:

    /\^/
    

    This will make it be interpreted as a literal ^ character.

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