JavaScript split String with white space

Asked
Active3 hr before
Viewed126 times

7 Answers

splitstringjavascript
90%

4 don't you mean whitespace or comma? – KaptajnKold Apr 27 '12 at 7:52 ,Thanks for contributing an answer to Stack Overflow!,… \s* matches zero or more white space characters (not just spaces, but also tabs and newlines).,... [\s,] matches one white space character or one comma

String.split() can also accept a regular expression:

input.split(/[ ,]+/);
load more v
88%

Search Answer Titles

var text = "hoi how     are          you";
var arr = text.split(/\s+/);
load more v
72%

I would like to split a String but I would like to keep white space like:,You should also make sure it is followed by whitespace s. so that strings like "My car isn't red" still work:,For split string by space like in Python lang, can be used:,You could split the string on the whitespace and then re-add it, since you know its in between every one of the entries.

I would like to split a String but I would like to keep white space like:

var str = "my car is red";

var stringArray[];

stringArray[0] = "my";
stringArray[1] = " ";
stringArray[2] = "car";
stringArray[3] = " ";
stringArray[4] = "is";
stringArray[5] = " ";
stringArray[6] = "red";
load more v
65%

The split() method splits a string into an array of substrings, and returns the new array.,If an empty string ("") is used as the separator, the string is split between each character.,Split a string into an array of substrings:,The split() method does not change the original string.

Definition and Usage

The split() method splits a string into an array of substrings, and returns the new array.

split()
load more v
75%

If separator is an empty string (""), str is converted to an array of each of its UTF-16 "characters". , When found, separator is removed from the string, and the substrings are returned in an array. , If the separator is an array, then that Array is coerced to a String and used as a separator. , An Array of strings, split at each point where the separator occurs in the given string.

split()
split(separator)
split(separator, limit)
load more v
40%

separator (optional): the separator is a pattern that will be used to divide the string (e.g. blank spaces, semicolons, http:// etc.).,The separator parameter allows the use of regular expressions.,Tip: to edit out the blank spaces at the beginning of the sentences use the array iteration method map() to apply the trim() method to each sentence.,You can also pass an array as the value for the separator parameter. When doing so, the array is converted to a string before being used as a separator, as in the example below:

The code below demonstrates how to use the split() method:

const str = 'Form an array from this string';

const arrCopy = str.split();
console.log(arrCopy);
// expected output: ["Form an array from this string"]

const words = str.split(' ');
console.log(words);
// expected output: ["Form", "an", "array", "from", "this", "string"]
load more v
22%

Method 1: Using split() and join() Method: The split() method is used to split a string into multiple sub-strings and return them in the form of an array. A separator can be specified as a parameter so that the string is split whenever that separator is found in the string. The space character (” “) is specified in this parameter to separate the string whenever a space occurs.,Method 2: Using replace() method with regex: The replace() method is used to replace a specified string with another string. It takes two parameters, first is the string to be replaced and the second parameter is the string replaced with. The second string can be given as empty string so that the empty space to be replaced.,The first parameter is given a regular expression with a space character (” “) along with the global property. This will select every occurrence of space in the string and it can then be removed by using an empty string in the second parameter. This will remove all the spaces in the original string.,Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Syntax:

string.split(" ").join("")

Syntax:

string.replace(/ /g, "")
load more v

Other "split-string" queries related to "JavaScript split String with white space"