你如何匹配正则表达式中的插入符号(^)?

时间:2023-01-14 14:00:32

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

我知道这些不会这样做,也是为什么,但你如何匹配呢?

/^/
/(^)/
/[^]/

2 个解决方案

#1


26  

Escape it with a backslash:

用反斜杠逃脱它:

/\^/

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

这将使其被解释为文字^字符。

#2


4  

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

我认为这有效(我在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

你可以在这里找到更多信息:http://www.regular-expressions.info/characters.html

#1


26  

Escape it with a backslash:

用反斜杠逃脱它:

/\^/

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

这将使其被解释为文字^字符。

#2


4  

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

我认为这有效(我在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

你可以在这里找到更多信息:http://www.regular-expressions.info/characters.html