代码链接 demo如下,定义好rule后进行parse。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 const rule = AndRule.of( [ TextRule.of("hello", "HELLO_RULE"), OrRule.of( [TextRule.of(", "), TimesRule.of(3, TextRule.of(" ", "SPACE"), "TIMES")], "NO_NAME" ), TextRule.of("world", "WORLD"), OneOrMoreRule.of(TextRule.of("!"), "SAMPLE") ], "HELLO_WORLD" ); console.log(JSON.stringify(rule.accept("hello world!!!"))); 结果为