javascript GWT:如何让正则表达式(模式和匹配器)在客户端工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4158201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:32:56  来源:igfitidea点击:

GWT : how to get regex(Pattern and Matcher) working in client side

javajavascriptregexgwt

提问by anjanb

We're using GWT 2.03 along with SmartGWT 2.2. I'm trying to match a regex like below in client side code.

我们使用 GWT 2.03 和 SmartGWT 2.2。我正在尝试在客户端代码中匹配如下所示的正则表达式。

Pattern pattern = Pattern.compile("\\"(/\d+){4}\\"");
String testString1 = "[    \"/2/4/5/6/8\",    \"/2/4/5/6\"]";
String testString2 = "[  ]";

Matcher matcher = pattern.matcher(testString1);
boolean result = false;
while (matcher.find()) {
    System.out.println(matcher.group());
}

It appears that Pattern and Matcher classes are NOT compiled to Javascript by the GWTC compiler and hence this application did NOT load. What is the equivalent GWT client code so that I can find regex matches within a String ?

看起来 Pattern 和 Matcher 类没有被 GWTC 编译器编译成 Javascript,因此这个应用程序没有加载。什么是等效的 GWT 客户端代码,以便我可以在 String 中找到正则表达式匹配?

How have you been able to match regexes within a String in client-side GWT ?

您是如何在客户端 GWT 中匹配字符串中的正则表达式的?

Thank you,

谢谢,

回答by glenn

Just use the String class to do it! Like this:

只需使用 String 类即可!像这样:

String text = "google.com";
    if (text.matches("(\w+\.){1,2}[a-zA-Z]{2,4}"))
        System.out.println("match");
    else
        System.out.println("no match");

It works fine like this, without having to import or upgrade or whatever. Just change the text and regex to your liking.

它像这样工作得很好,无需导入或升级或其他任何东西。只需根据自己的喜好更改文本和正则表达式即可。

Greetings, Glenn

你好,格伦

回答by Adrian Smith

Consider upgrading to GWT 2.1 and using RegExp.

考虑升级到 GWT 2.1 并使用RegExp.

回答by Peter Knego

Use GWT JSNIto call native Javascript regexp:

使用GWT JSNI调用原生 Javascript regexp:

public native String jsRegExp(String str, String regex)
/*-{
    return str.replace(/regex/);  // an example replace using regexp 
    }
}-*/;

回答by Adrian Smith

Perhaps you could download the RegExp files from GWT 2.1 and add them to your project?

也许您可以从 GWT 2.1 下载 RegExp 文件并将它们添加到您的项目中?

http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/regexp/

http://code.google.com/p/google-web-toolkit/source/browse/trunk/user/src/com/google/gwt/regexp/

Download GWT 2.1 incl source, add that directory somewhere in your project, then add the reference to the "RegExp.gwt.xml" using the <inherits>tag from your GWT XML.

下载 GWT 2.1 包含源代码,将该目录添加到项目中的某个位置,然后使用<inherits>GWT XML 中的标记添加对“RegExp.gwt.xml”的引用。

I'm not sure if that would work, but it'd be worth a shot. Maybe it references something else GWT 2.1 specific which you don't have, but I've just checked out the code a bit and I don't think it does.

我不确定这是否可行,但值得一试。也许它引用了您没有的其他特定于 GWT 2.1 的东西,但我刚刚检查了一些代码,我认为它没有。

回答by StackOverFlow

GWT 2.1 now has a RegExpclass that might solve your problem:

GWT 2.1 现在有一个RegExp类可以解决您的问题:

// Compile and use regular expression
RegExp regExp = RegExp.compile(patternStr);
MatchResult matcher = regExp.exec(inputStr);
boolean matchFound = regExp.test(inputStr);

if (matchFound) {
Window.alert("Match found");
    // Get all groups for this match
    for (int i=0; i<=matcher.getGroupCount(); i++) {
        String groupStr = matcher.getGroup(i);
        System.out.println(groupStr);
    }
}else{
Window.alert("Match not found");
}