[英]split a string using javascript
I have a string like ";a;b;c;;e". 我有一个像“; a; b; c ;; e”这样的字符串。 Notice that there is an extra semicolon before
e
. 请注意,在
e
之前有一个额外的分号。 I want the string to be split in a
, b
, c;
我希望将字符串拆分为
a
, b
, c;
, e
. ,
e
。 But it gets split like a
, b
, c
, ;e
. 但它像
a
, b
, c
, ;e
那样分裂。
My code is 我的代码是
var new_arr = str.split(';');
What can I do over here to get the result I want? 我能在这做什么来获得我想要的结果?
Regards 问候
使用Regexp负向前瞻:
";a;b;c;;e".split(/;(?!;)/)
Interesting, I get ["", "a", "b", "c", "", "e"]
with your code. 有趣的是,我的代码中包含
["", "a", "b", "c", "", "e"]
。
var new_array = ";a;b;c;;e".split(/;(?!;)/);
new_array.shift();
This works in Firefox, but I think it's correct. 这适用于Firefox,但我认为这是正确的。 You might need this cross-browser split for other browsers.
对于其他浏览器,您可能需要这种跨浏览器拆分 。
var myArr = new Array();
var myString = new String();
myString = ";a;b;c;;e";
myArr = myString.split(";");
for(var i=0;i<myArr.length;i++)
{
document.write( myArr[i] );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.