文字列が正規表現と段階的に (一度に 1 文字ずつ) 一致するかどうかを確認し、不確定な結果を返すことができる JavaScript ライブラリ (理想的には node.js パッケージ) を探しています。たとえば、次の正規表現があるとします。
j.*s.*
そして、文字列「javascript」をテストしたいと思います。次のような API が必要です。
var iregex = new IncrementalRegex('j.*s.*');
var matcher = iregex.createMatcher();
matcher.append('j');
matcher.test(); //returns "possible match"
matcher.append('a');
matcher.test(); //returns "possible match"
matcher.append('v'); matcher.append('a'); matcher.append('s');
matcher.test(); //returns "match found"
matcher.append('ript');
matcher.test(); //returns "match found"
文字列「foo」をテストすると、次のようになります。
var matcher2 = iregex.createMatcher();
matcher.append('f');
matcher.test(); //returns "no match possible"
//At this point I wouldn't bother appending "oo" because I know that no match is possible.
編集:明確にするために、追加はテスト対象の文字列を構築しています。新しいマッチャーは空の文字列に対してテストを開始し、matcher.append('foo') の後に foo と照合します。appendToString または buildUpString の方が適切な名前である可能性があります。
また、これをどのように実行できるかについて 1 つの考えがありますが、まだ完全には考えていません。おそらく、元の正規表現が一致する文字列の先頭である場合にのみ、文字列に一致する元の正規表現から「潜在的な一致」正規表現を構築することが可能です。