Variable for an array?

I have a variable that looks like this: var regVar="Right Left Right;"And then I split them: regVar.split();So what I'm trying to do is make the variable with the name regVarbe converted to an array. I thought there was a method .array(), but I could not understand how this works. If there is a jQuery method, it will be fantastic as well as simple JS. Thank you for your help!

+4
source share
4 answers

To break a string with spaces, use .split(" ");

You split the string into anything just by passing a separator (i.e. a separator) as an argument split().

:
"hello world".split(' ') ["hello", "world"]
"google.com".split('.') ["google", "com"]
"415-283-8927".split('-') ["415", "283", "8927"]

Bonus:
, :
"helloworld".split('') ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] ( , )

, (.split()), , .

+5

, , , , :

var regVar="Right Left Right;"
var arrayOfRegVar = regVar.split(" ");

console.log(arrayOfRegVar); //["Right", "Left", "Right"];
+7

RegVar

var regVar="Right Left Right;"
var regVar = regVar.split(" ");

regVar

regVar = ['Right','Left','Right'];
+2

split() JavaScript, .

myString.split(separator, limit);

where separator is optional and indicates the character or regular expression to use to separate the string. If this parameter is omitted, an array with one element is returned. and limit (optional) is an integer that determines the number of partitions, elements after the dividing limit is not included in the array.

In your case use

regVar = regVar.split(" ");

It will give you the desired result ie ["Right", "Left", "Right"];

+2
source

All Articles