1

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

3 Answers 3

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*/)
Sign up to request clarification or add additional context in comments.

2 Comments

alright...initially that's what I had. Just second guessed myself...not real great with JS yet. I can rely absolute on the "}, {" being the split but don't I have to use a reg expression to make sure the },{ isn't interpreted as something else?
@Verber: If there could be some other }, { in the string that should not be a split token, then .split() won't work in general unless there's some other characters to differentiate them.
0

var arr = str.split(/[\{\},\s]+/)

Comments

0
var s = 'Hello"}, {"World"}, {"From"}, {"Ohio';
var a = s.split('"}, {"');
alert(a);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.