正则表达式以捕获逗号分隔的值 [英] Regexp to capture comma separated values

查看:192
本文介绍了正则表达式以捕获逗号分隔的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,它可以是逗号分隔的 \ w 列表,例如:

I have a string that can be a comma separated list of \w, such as:

  • abc123
  • abc123,def456,ghi789

我正在尝试找到一个JavaScript正则表达式,它将返回 ['abc123'] (第一种情况)或 ['abc123','def456','ghi789'] (不带逗号).

I am trying to find a JavaScript regexp that will return ['abc123'] (first case) or ['abc123', 'def456', 'ghi789'] (without the comma).

我尝试过:

  • ^(\ w + ,?)+ $ -不,因为只有最后一个重复模式会被匹配789
  • ^(?:(\ w +),?)+ $ -相同的故事.我正在使用非捕获支架.但是,对于重复的单词来说,捕获似乎并没有发生
  • ^(\w+,?)+$ -- Nope, as only the last repeating pattern will be matched, 789
  • ^(?:(\w+),?)+$ -- Same story. I am using non-capturing bracket. However, the capturing just doesn't seem to happen for the repeated word

我正尝试使用regexp做什么吗?我几乎对分组的每种组合都进行了尝试,使用了捕获和不捕获的括号,但仍然没有设法做到这一点...

Is what I am trying to do even possible with regexp? I tried pretty much every combination of grouping, using capturing and non-capturing brackets, and still not managed to get this happening...

推荐答案

如果您想在出现问题时放弃整个输入,最简单的方法是进行验证,然后拆分:

If you want to discard the whole input when there is something wrong, the simplest way is to validate, then split:

if (/^\w+(,\w+)*$/.test(input)) {
    var values = input.split(',');

    // Process the values here
}

如果要允许为空值,请将 \ w + 更改为 \ w * .

If you want to allow empty value, change \w+ to \w*.

尝试使用单个正则表达式同时进行匹配和验证需要模拟 \ G 功能,该功能可以断言最后一次匹配的位置.为什么需要 \ G ?因为这样可以防止引擎在下一个位置重试比赛并绕过您的验证.请记住,ECMA Script正则表达式没有后顾之忧,因此您无法区分无效字符的位置和无效字符之后的字符:

Trying to match and validate at the same time with single regex requires emulation of \G feature, which assert the position of the last match. Why is \G required? Since it prevents the engine from retrying the match at the next position and bypass your validation. Remember than ECMA Script regex doesn't have look-behind, so you can't differentiate between the position of an invalid character and the character(s) after it:

something,=bad,orisit,cor&rupt
          ^^             ^^

当您无法区分这两个位置时,就不能仅依靠引擎来进行全部匹配操作.虽然可以在 RegExp.exec 中使用while循环并自己声明最后一个匹配的位置,但是如果有更简洁的选择,为什么还要这么做呢?

When you can't differentiate between the 2 positions, you can't rely on the engine to do a match-all operation alone. While it is possible to use a while loop with RegExp.exec and assert the position of last match yourself, why would you do so when there is a cleaner option?

如果您想利用任何可用的方法, torazaburo的答案是一个可行的选择.

If you want to savage whatever available, torazaburo's answer is a viable option.

这篇关于正则表达式以捕获逗号分隔的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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