Example 1: javascript split by comma
<script>
var names = 'Harry,John,Clark,Peter,Rohn,Alice';
var nameArr = names.split(',');
console.log(nameArr);
alert(nameArr[0]);
alert(nameArr[1]);
alert(nameArr[nameArr.length - 1]);
var str = 'Hello World!';
var chars = str.split('');
console.log();
alert(chars[0]);
alert(chars[1]);
alert(chars[chars.length - 1]);
</script>
Example 2: javascript explode
var str = "Well, how, are , we , doing, today";
var res = str.split(",");
Example 3: string split javascript
var myString = "An,array,in,a,string,separated,by,a,comma";
var myArray = myString.split(",");
Example 4: javascript split
var names = 'Harry ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ';
console.log(names);
var re = /\s*(?:;|$)\s*/;
var nameList = names.split(re);
console.log(nameList);
Example 5: node js split
function splitString(stringToSplit, separator) {
var arrayOfStrings = stringToSplit.split(separator);
console.log('The original string is: "' + stringToSplit + '"');
console.log('The separator is: "' + separator + '"');
console.log('The array has ' + arrayOfStrings.length + ' elements: ' + arrayOfStrings.join(' / '));
}
var tempestString = 'Oh brave new world that has such people in it.';
var monthString = 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec';
var space = ' ';
var comma = ',';
splitString(tempestString, space);
splitString(tempestString);
splitString(monthString, comma);
Example 6: how to split a string in javascript
strName.split();