翻译中丢失-C#正则表达式到JavaScript正则表达式 [英] Lost in translation - C# regex to JavaScript regex

查看:62
本文介绍了翻译中丢失-C#正则表达式到JavaScript正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将我正在使用的C#正则表达式转换为JavaScript的正则表达式实现时遇到了麻烦.

I'm having some trouble translating my working C# regular expression into JavaScript's regular expression implementation.

这是正则表达式:

([a-z]+)((\d+)([a-z]+))?,?

"water2cups,four4cups,salt2teaspoon" 上使用时,您应该得到:

When used on "water2cups,flour4cups,salt2teaspoon" you should get:

[
    ["water", "2cups", "2", "cups"]
    ["flout", "4cups", "4", "cups"]
    ["salt", "2teaspoon", "2", "teaspoon"]
]

...而且确实如此.在C#中.但不是在JavaScript中.

... And it does. In C#. But not in JavaScript.

我知道各个实现之间存在一些细微差异.使该表达式在JavaScript中运行我想念的是什么?

更新

I know there are some minor differences across implementations. What am I missing to get this expression working in JavaScript?

Update

我正在像这样使用正则表达式:

I am using the regex like so:

"water2cups,flour4cups,salt2teaspoon".match(/([a-z]+)((\d+)([a-z]+))?,?/g);

推荐答案

创建RegExp

您没有显示如何正在创建Javascript正则表达式,例如,您是否使用文字:

Creating the RegExp

You haven't shown how you're creating your Javascript regular expression, e.g., are you using a literal:

var rex = /([a-z]+)((\d+)([a-z]+))?,?/;

或字符串

var rex = new RegExp("([a-z]+)((\\d+)([a-z]+))?,?");

如果是后者,请注意我已经避开了反斜杠.

If the latter, note that I've escaped the backslash.

默认情况下,JavaScript正则表达式不是全局的,这可能对您来说是个问题.如果尚未添加 g 标志:

By default, Javascript regular expressions are not global, that may be an issue for you. Add the g flag if you don't already have it:

var rex = /([a-z]+)((\d+)([a-z]+))?,?/g;

var rex = new RegExp("([a-z]+)((\\d+)([a-z]+))?,?", "g");

使用 RegExp#exec 而不是 String#match

您的编辑说您正在使用 String#match 来获取匹配项数组.我不得不承认,我几乎从不使用 String#match (如下所示,我使用 RegExp#exec .)当我将 String#match 与您的正则表达式,我得到...非常奇怪的结果,因浏览器而异.使用 RegExp#exec 循环不会那样做,所以这就是我要做的.

Using RegExp#exec rather than String#match

Your edit says you're using String#match to get an array of matches. I have to admit I hardly ever use String#match (I use RegExp#exec, as below.) When I use String#match with your regex, I get...very odd results that vary from browser to browser. Using a RegExp#exec loop doesn't do that, so that's what I'd do.

此代码可满足您的需求:

This code does what you're looking for:

var rex, str, match, index;

rex = /([a-z]+)((\d+)([a-z]+))?,?/g;
str = "water2cups,flour4cups,salt2teaspoon";

rex.lastIndex = 0; // Workaround for bug/issue in some implementations (they cache literal regexes and don't reset the index for you)
while (match = rex.exec(str)) {
    log("Matched:");
    for (index = 0; index < match.length; ++index) {
        log("&nbsp;&nbsp;match[" + index + "]: |" + match[index] + "|");
    }
}

( log 函数只是将文本追加到div.)

(The log function just appends text to a div.)

我的输出是:

Matched:
  match[0]: |water2cups,|
  match[1]: |water|
  match[2]: |2cups|
  match[3]: |2|
  match[4]: |cups|
Matched:
  match[0]: |flour4cups,|
  match[1]: |flour|
  match[2]: |4cups|
  match[3]: |4|
  match[4]: |cups|
Matched:
  match[0]: |salt2teaspoon|
  match[1]: |salt|
  match[2]: |2teaspoon|
  match[3]: |2|
  match[4]: |teaspoon|

(回想一下,在Javascript中, match [0] 将是整个匹配;然后 match [1] 等,是您的捕获组.)

(Recall that in Javascript, match[0] will be the entire match; then match[1] and so on are your capture groups.)

这篇关于翻译中丢失-C#正则表达式到JavaScript正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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