Stephan K.
Stephan K.

Reputation: 15702

Regex JavaScript String and store results into variables

If I had a string with three values that is delimited by spaces, now I want to store these three values in three variables or maybe an array, how to do?

Upvotes: 0

Views: 249

Answers (2)

Stephan K.
Stephan K.

Reputation: 15702

Got it: I use String.split(pattern)

var str = "I am confused".split(/\s/g)

Upvotes: 0

Jason McCreary
Jason McCreary

Reputation: 72981

Use split().

For example:

var variables = delimited_string.split(/\s+/);

If you know it is separated by a single space, avoid using a regular expression with the following:

var variables = delimited_string.split(' ');

Upvotes: 2

Related Questions