在表单中有一个文本字段,我想在其中限制“^”符号。我试图在正则表达式中转义 carret 符号 '^'。例如
"abcdef".match([^])
正在返回 true
请提供建议。
在表单中有一个文本字段,我想在其中限制“^”符号。我试图在正则表达式中转义 carret 符号 '^'。例如
"abcdef".match([^])
正在返回 true
请提供建议。
要匹配行开头:
> 'abcdef'.match(/^/)
[ '', index: 0, input: 'abcdef' ]
要匹配 literal ^
,请将其转义:
> 'abcdef'.match(/\^/)
null
要匹配^
一类字符中的文字,请将其放在除第一个以外的任何位置:
> 'abcdef'.match(/[xyz^]/)
null
> 'abcdef'.match(/[def^]/)
[ 'd', index: 3, input: 'abcdef' ]
使用.search(/\^/)
.Backslash '\' 将删除 '^' 的功能。这样你可以限制。
语法错误。正则表达式必须包含/
在 JS 中,所以它应该是
"abcdef".match("/[^]/"); //gives null
此外,您不需要将其包含在/
a[]
中,您可以使用以下命令将其转义\
:
"abcdef".match("/\^/"); //gives null
有关详细信息,请参阅http://www.regular-expressions.info/javascript.html
如果您只想检查字符串是否包含插入符号,请尝试
/\^/.test( "abcdef" ); // => false
/\^/.test( "^abcdef" ); // => true
/[^\^]/.test( "aslkfdjfs" ); // =>true as caret does not exist in string