split every character javascript code example
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: split every n character js
var str = 'abcdefghijkl';
console.log(str.match(/.{1,3}/g));
Example 3: js split string every n characters
foo.match(new RegExp('.{1,' + n + '}', 'g'));
Example 4: split a string every n characters javascript
console.log("abcd".match(/.{1,2}/g));
Example 5: javascript explode space
var str = "my car is red";
var stringArray = str.split(/(\s+)/);
console.log(stringArray);