这是自动配对星号(用于Markdown和AsciiDoc)文件的按键图。有用。问题:
1)我是否应该在第一个区块的第六行中避开星号?
2)在我的测试中,此行的转义没有任何区别。但为什么?如果您尝试删除最后一个块的第5行和第6行(以及第5个块中的行)中的转义,将导致错误的行为。因此,此处需要转义 。但似乎在第一个块中并不需要。这让我感到困惑。
[
// Auto-pair *
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*$0*"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|\\}|>|$)", "match_all": true },
{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "[\\*a-zA-Z0-9_]$", "match_all": true }, // --- THIS line ---
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.other - punctuation.definition.string.end", "match_all": true }
]
},
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*${0:$SELECTION}*"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
]
},
{ "keys": ["*"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\*", "match_all": true }, // --- THIS line ---
{ "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.other - punctuation.definition.string.end", "match_all": true },
]
},
{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Left Right.sublime-macro"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\*$", "match_all": true }, // --- THIS line ---
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\*", "match_all": true }, // --- THIS line ---
{ "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.other - punctuation.definition.string.end", "match_all": true },
]
},
]
更新:实际上,最好使用另一个正则表达式:
- [*a-zA-Z0-9_]$
+ (^[*\\s]*$)|([*a-zA-Z0-9_]$)
这样,当您使用项目符号列表时,星号将不会在行的开头重复。
答案 0 :(得分:1)
在正则表达式中,*
运算符很特殊,表示前一个原子的<零>个或多个重复。因此,为了匹配文字*
,您需要将其转义为\*
(或在JSON \\*
中)以向正则表达式引擎表明您是文字*
而不应用特殊含义。
构造[]
代表character set,它与集合中的任何字符匹配。在字符集内,对正则表达式引擎具有特殊含义的唯一字符是]
(关闭字符集),\
(仍需要转义),-
(指定一定范围的字符)和^
(否定集合,并且只有在第一个字符时才是特殊字符),因此只有那些字符需要在集合内转义。
由于字符集的意思是匹配以下任意一个字符,*
的特殊含义不适用,因此在字符集内部无需转义(尽管您仍然可以这样做)。