简体   繁体   English

用多个分隔符分割字符串

[英]String split with multiple separators

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\{\/");
}

First, get rid of the while loop. 首先,摆脱while循环。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM