Check if string is a prefix of a Javascript RegExp

后端 未结 5 2119
旧巷少年郎
旧巷少年郎 2021-01-04 03:31

In Javascript I have defined a regular expression and now a user is typing in a string. I want to tell him if his string still could match the RegExp if he continues typing

5条回答
  •  天涯浪人
    2021-01-04 04:11

    Very interesting question. In my quick search, I didn't find anything predefined (not even in Perl) that solves this problem.

    EDIT: Ouch, it seems Java has something similar called hitEnd() -- see Alan M's answer. What hitEnd() does is say that the result of match() (either true or false) might be modified by additional input. The book 'Mastering Regular Expressions" says it's not very reliable though (not sure why, page 392 not available in google books).

    Depending on what features of regular expressions you use, a quick hack like writing some sort of prefixes of your regexp:

    e.g. for a+a*b+c your prefixes will be:

    a+
    a+a*
    a+a*b+
    a+a*b+c
    

    and try to match any of them with your string might work. This quick hack is made difficult if you use the choice operator, if you use the range operator {n,m} or back-references.

    That being said, I think the good solution is to slightly modify the matching algorithm.

    The matching algorithm normally employed is a backtracking algorithm (which works well in practice, even if worst case behavior is exponential). This algorithm successfully terminates whenever it has reached the end of the regexp (even if not the entire string was consumed). What you need to do is to modify the termination condition such that it also terminates successfully when it has consumed all of the input.

    That being said, you'd probably have to actually implement the algorithm in JavaScript. Hopefully this will become part of libraries such as Jquery.

    For more references and theory on the algorithm, check this article out:

    http://swtch.com/~rsc/regexp/regexp1.html

    (even if it makes a case against the backtracking algorithm and suggests a FA based algorithm (but the FA cannot handle back-references)).

提交回复
热议问题