javascript - String split with multiple separators - Stack Overflow

I'm trying to split a huge string that uses "}, {" as it's separator. If I use the

I'm trying to split a huge string that uses "}, {" as it's separator.

If I use the following code will I get split it into it's own string?

var i;
var arr[];
while(str) {
    arr[i] = str.split("/^}\,\s\{\/");
}

I'm trying to split a huge string that uses "}, {" as it's separator.

If I use the following code will I get split it into it's own string?

var i;
var arr[];
while(str) {
    arr[i] = str.split("/^}\,\s\{\/");
}
Share Improve this question asked Sep 27, 2012 at 16:48 John VerberJohn Verber 7552 gold badges16 silver badges32 bronze badges 3
  • @AustinBrunkhorst -- Good call. – Jeremy J Starcher Commented Sep 27, 2012 at 16:59
  • What do you mean by "get split it into its own string"? – Anderson Green Commented Jun 10, 2013 at 23:11
  • possible duplicate of How do I split a string with multiple separators in javascript? – Anderson Green Commented Aug 4, 2013 at 19:41
Add a ment  | 

3 Answers 3

Reset to default 6

First, get rid of the while loop. Strings are immutable, so it won't change, so you'll have an infinite loop.

Then, you need to get rid of the quotation marks to use regex literal syntax and get rid of the ^ since that anchors the regex to the start of the string.

/},\s\{/

Or just don't use a regex at all if you can rely on that exact sequence of characters. Use a string delimiter instead.

"}, {"

Also, this is invalid syntax.

var arr[];

So you just do the split once, and you'll end up with an Array of strings.

All in all, you want something like this.

var arr = str.split(/*your split expression*/)

var arr = str.split(/[\{\},\s]+/)
var s = 'Hello"}, {"World"}, {"From"}, {"Ohio';
var a = s.split('"}, {"');
alert(a);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744718448a4589765.html

相关推荐

  • javascript - String split with multiple separators - Stack Overflow

    I'm trying to split a huge string that uses "}, {" as it's separator. If I use the

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信