Q:

javascript split regex


var str = "How are you doing today?";

var res = str.split(" ");
 
7
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.split(''));
console.log(str.split(' '));
console.log(str.split('ox'));
> Array ["T", "h", "e", " ", "q", "u", "i", "c", "k", " ", "b", "r", "o", "w", "n", " ", "f", "o", "x", " ", "j", "u", "m", "p", "s", " ", "o", "v", "e", "r", " ", "t", "h", "e", " ", "l", "a", "z", "y", " ", "d", "o", "g", "."] 
> Array ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."] 
> Array ["The quick brown f", " jumps over the lazy dog."]
3
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split(' ');
console.log(words[3]);
// expected output: "fox"

const chars = str.split('');
console.log(chars[8]);
// expected output: "k"

const strCopy = str.split();
console.log(strCopy);
// expected output: Array ["The quick brown fox jumps over the lazy dog."]
0
str.split([separator[, limit]])
0
str.split(separator/*, limit*/)

/*Syntax explanation -----------------------------
Returns an array of strings seperated at every 
point where the 'separator' (string or regex) occurs. 
The 'limit' is an optional non-sub-zero integer. 
If provided, the string will split every time the 
separator occurs until the array 'limit' has been reached.
*/

//Example -----------------------------------------
const str = 'I need some help with my code bro.';

const words = str.split(' ');
console.log(words[6]);
//Output: "code"

const chars = str.split('');
console.log(chars[9]);
//Output: "m"

const limitedChars = str.split('',10);
console.log(limitedChars);
//Output: Array ["I", " ", "n", "e", "e", "d", " ", "s", "o", "m"]

const strCopy = str.split();
console.log(strCopy);
//Output: Array ["I need some help with my code bro."]
0

New to Communities?

Join the community