【问题标题】:Recursive Regex expression to split expression by character [duplicate]递归正则表达式按字符拆分表达式[重复]
【发布时间】:2016-04-22 11:27:04
【问题描述】:

我正在寻找一个正则表达式来拆分以下类型的数据:

<exp>(;<exp>)*

并接收每个 exp 的列表,但不接收分隔符 ';'。

到目前为止,我有 ([^;])(?:;([^;])) 但是这个正则表达式在 ';' 之后的第一个 exp 处停止.有没有办法让前瞻无限?

这是一个具体的例子:

23;hello;2452;ad34aa;bye

我想得到:

[0] = 23
[1] = hello
[2] = 2452
[3] = ad34aa
[4] = bye

如果此问题已得到解答,我深表歉意,但我无法找到有效的表达方式。如果有请告诉我链接。

感谢您花时间阅读本文:)

【问题讨论】:

  • 不会被;拆分给你吗?
  • split(';')match(/[^;]+/g)
  • "Recursive"、"lookahead" - 这些术语在问题中被滥用。
  • 为了避免再次犯类似的错误,您建议用什么词来定义这个问题/问题?

标签: javascript regex


【解决方案1】:

使用String#split

var array = '23;hello;2452;ad34aa;bye'.split(';');
document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');

// as mentioned from Avinash Raj
var array2 = '23;hello;2452;ad34aa;bye'.match(/[^;]+/g);
document.write('<pre>' + JSON.stringify(array2, 0, 4) + '</pre>');

【讨论】:

  • 还有str.match(/[^;]+/g)
  • String.split 假定使用 javascript 语言,我正在寻找一个正则表达式,版本正是我想要的,谢谢。
猜你喜欢
  • 1970-01-01
  • 2018-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多