检查字符串是否为Javascript RegExp的前缀 [英] Check if string is a prefix of a Javascript RegExp

查看:44
本文介绍了检查字符串是否为Javascript RegExp的前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Javascript中,我定义了一个正则表达式,现在用户正在输入字符串.我想告诉他,如果他继续输入或使用错误的方式,他的字符串是否仍可以匹配RegExp.例如:

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 or if he's already on the wrong way. For instance:

var re = /a*b/;

"a".isPrefixOf( re ); // true
"x".isPrefixOf( re ); // false

isPrefixOf 的实现看起来如何?

更新:感谢您的回答,按照布拉德(Brad)的建议,使正则表达式前缀可靠,这似乎是一个不错的解决方法.但是我仍在尝试找到一个通用的解决方案.

Update: Thanks for your answers, making the regex prefix-proof, as suggested by brad, seems to be a good workaround. But I'm still trying to find a general solution.

也许是这样:我们用用户输入后跟.* 创建一个新的正则表达式.此正则表达式描述了用户仍然可以输入的所有单词.如果此创建的正则表达式与原始正则表达式的交集为空,则用户已经走错了路.如果不是,那他就没事了.例如:

Maybe this way: We create a new regex with the user input followed by .*. This regex describes all words that the user still may enter. If the intersection of this created regex and the original regex is empty then the user is already on the wrong way. If it's not, he's doing fine. For instance:

var re = /a*b/;
var sInput = "a";
var reInput = new RegExp( sInput + ".*" );

reIntersection = re.intersect( reInput );
reIntersection.isEmpty(); // false

intersect()返回一个新的正则表达式,该正则表达式仅接受 re reInput 都将接受的单词.该功能尚不存在,但我们可以使用超前实现:

intersect() returns a new regex that accepts only word which both re and reInput would accept. The function doesn't exist yet but we can implement it using look-ahead:

RegExp.prototype.intersect = function( pattern2 ) { 
    return new RegExp( '(?=' + this.source  + ')' + pattern2.source );
}

仍然打开的是 isEmpty()函数.我们如何检查Javascript正则表达式是否与任何单词匹配或为空?

What remains open is the isEmpty() function. How could we check, if a Javascript regex matches any word or if it's empty?

推荐答案

人们对于他们对这个问题的理解似乎各有不同,所以我将以Java示例为例进行演示.

People seem to be splitting evenly on how they interpret this question, so I'll demonstrate the concept with a Java example.

import java.util.regex.*;

public class Test
{

  public static void main(String[] args) throws Exception
  {
    tryMatch("^a*b+$", "a", "ab", "abc");
  }

  public static void tryMatch(String regex, String... targets)
  {
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher("");
    System.out.printf("%nregex: %s%n", regex);
    System.out.printf("target | matches() | hitEnd()%n");
    for (String str : targets)
    {
      m.reset(str);
      System.out.printf("%-6s | %-9B | %-9B%n",
          str, m.matches(), m.hitEnd());
    }
  }
}

输出:

regex: ^a*b+$
target | matches() | hitEnd()
a      | FALSE     | TRUE
ab     | TRUE      | TRUE
abc    | FALSE     | FALSE

目标字符串"a"不匹配,因为正则表达式至少需要一个 b ,但是它可能是成功匹配的前缀,因此 hitEnd()返回 true .字符串"ab"具有匹配所需要的全部内容,但是如果我们在末尾添加更多的 b ,它也将匹配,因此 hitEnd()仍返回 true .使用"abc",匹配尝试在到达目标字符串的末尾之前会失败,因此正则表达式无法匹配任何以"abc"开头的字符串.

Target string "a" doesn't match because the regex requires at least one b, but it could be the prefix of a successful match, so hitEnd() returns true. String "ab" has all that's required for a match, but it would also match if we added more b's to the end, so hitEnd() still returns true. With "abc" the match attempt fails before it reaches the end of the target string, so the regex couldn't match any string that starts with "abc".

据我所知,Javascript没有像Java的 hitEnd()方法那样的东西,但是可能会伪造它.如果有人知道,那将是Flagrant Badass, Steven Levithan .

As far as I know, Javascript doesn't have anything like Java's hitEnd() method, but it might be possible to fake it. If anyone knows how, it'll be that Flagrant Badass, Steven Levithan.

这篇关于检查字符串是否为Javascript RegExp的前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆