split on regex js code example
Example 1: string split javascript
var myString = "An,array,in,a,string,separated,by,a,comma";
var myArray = myString.split(",");
/*
*
* myArray :
* ['An', 'array', 'in', 'a', 'string', 'separated', 'by', 'a', 'comma']
*
*/
Example 2: javascript split regex
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."]
Example 3: split() javascript
// It essentially SPLITS the sentence inserted in the required argument
// into an array of words that make up the sentence.
var input = "How are you doing today?";
var result = input.split(" "); // Code piece by ZioTino
// the following code would return as
// string["How", "are", "you", "doing", "todaY"];