使用 codemirror 的简单模式的奇怪的正则表达式行为

Weird regex behavior using codemirror's simple mode

我正在尝试创建一种允许 Todo.txt 格式的模式,该格式非常非常简单,但在 codemirror 匹配结果时具有奇怪的正则表达式行为。

基本上,我试图标记所有以 "x" 开头的行,然后是 space 字符,并且仅在行首具有此条件,但正则表达式在中间选择一个字符串该行。

尝试使用其他 javascript 正则表达式工具时,我的正则表达式不符合此条件:https://regex101.com/r/kUXTqf/1

这是简单模式定义中的正则表达式行:

{regex: /^(x ).*$/, token: "task-completed"} 

我正在测试的文本:

x 2017-12-12 @geek add file location preference option +todotxtapp
(A) @geek completed task syntax highlighter rule needs tweak - it includes any character follows with whitespace - starting in the middle of the line +todotxtapp
(B) @geek design new app icon +todotxtapp
(C) @geek add priority shortcut cmd+up/down or similar +todotxtapp
asdasdasdasdasa x dsljhdsfkljg dhsklf sdaf

实际中只需要匹配第一行即可。但它匹配第二行和最后一行的一半。 在此处查看结果:http://take.ms/S2PEL

我不熟悉 CodeMirror,但是没有文档,

Simple modes (loosely based on the Common JavaScript Syntax Highlighting Specification, which never took off), are state machines, where each state has a number of rules that match tokens.

正则表达式不适用于行,它适用于令牌。因此,asdasdasdasdasaxdsljhdsfklhg都是单独测试的;不出所料,x 匹配 /^(x ).*$/.

看来你想要这样的东西(你可能需要调整它,因为我无法测试它):

{regex: /x/, token: "task-completed", sol: true} 

sol: boolean

When true, this token will only match at the start of the line. (The ^ regexp marker doesn't work as you'd expect in this context because of limitations in JavaScript's RegExp API.)

编辑:我必须说,我不太确定 syntax 发生了什么。