JAVASCRIPT:正则表达式在多个|之间找到字符|

时间:2013-12-08 14:29:37

标签: javascript regex split

如何匹配2个字符(| |)之间的所有字符,例如

INPUT:

|one|two|three|

输出:

'one','two','three'.

2 个答案:

答案 0 :(得分:1)

您不需要正则表达式:

> '|one|two|three|'.split('|')
["", "one", "two", "three", ""]
> '|one|two|three|'.split('|').slice(1, -1)
["one", "two", "three"]

答案 1 :(得分:0)

使用javascript正则表达式,你可以尝试类似的东西,

var text = "|one|two|three|";
var r = new RegExp("(\\|)?([\\w]*)(?=\\|)","g");
var res=r.exec(text);
while(res&&res[2]){
  console.log(res[2]);
  res=r.exec(text);
}

http://jsfiddle.net/gMuDy/