在窗體中有一個文本字段,我想限制'^'標誌。 我試圖在正則表達式中逃避carret標誌'^'。 例如在正則表達式中逃脫carret標誌'^'
"abcdef".match([^])
被返回true
請提供建議。
在窗體中有一個文本字段,我想限制'^'標誌。 我試圖在正則表達式中逃避carret標誌'^'。 例如在正則表達式中逃脫carret標誌'^'
"abcdef".match([^])
被返回true
請提供建議。
語法錯誤。正則表達式必須括在JS /
,所以它應該是
"abcdef".match("/[^]/"); //gives null
而且,你不需要到/
攬入[]
,你可以用\
逃避它:
"abcdef".match("/\^/"); //gives null
要匹配行開始:
> 'abcdef'.match(/^/)
[ '', index: 0, input: 'abcdef' ]
要匹配字面^
,逃吧:
> 'abcdef'.match(/\^/)
null
要匹配字面^
一類的人物裏面,把它放在任何位置,除了第一:
> 'abcdef'.match(/[xyz^]/)
null
> 'abcdef'.match(/[def^]/)
[ 'd', index: 3, input: 'abcdef' ]
如果你只是想檢查字符串是否包含脫字符號碼
/\^/.test("abcdef"); // => false
/\^/.test("^abcdef"); // => true
/[^\^]/.test("aslkfdjfs"); // =>true as caret does not exist in string
使用.search(/\^/)
。反斜槓'\'將刪除'^'的功能。這樣您可以限制。