Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string on the first white space occurrence

People also ask

How do you split a string after first space?

Use the String. split() method with array destructuring to split a string only on the first occurrence of a character, e.g. const [first, ... rest] = str. split('-'); .

How do you split a string in white space?

The standard solution to split a string is using the split() method provided by the String class. It accepts a regular expression as a delimiter and returns a string array. To split on any whitespace character, you can use the predefined character class \s that represents a whitespace character.

Does split () alter the original string?

Note: The split() method does not change the original string. Remember – JavaScript strings are immutable. The split method divides a string into a set of substrings, maintaining the substrings in the same order in which they appear in the original string. The method returns the substrings in the form of an array.


If you only care about the space character (and not tabs or other whitespace characters) and only care about everything before the first space and everything after the first space, you can do it without a regular expression like this:

str.substr(0,str.indexOf(' ')); // "72"
str.substr(str.indexOf(' ')+1); // "tocirah sneab"

Note that if there is no space at all, then the first line will return an empty string and the second line will return the entire string. Be sure that is the behavior that you want in that situation (or that that situation will not arise).

Somewhat pedantic update: Although it is supported in effectively all browsers as well as Node.js, deno, etc., String.prototype.substr() has never been added normatively to the ECMAScript spec. Practically, this is unlikely to affect you. However, if it bothers you (or if you are running in some resource-constrained environment that doesn't have String.prototype.substr() for some reason), you can use one of the many other fine answers people have provided on this question. String.prototype.slice() is a pretty good substitute but be careful of the weirdness that ensues if indexOf() returns -1 for the second argument to slice(). (It will truncate the last character of the string.) Personally, I like the regexp lookbehind solution at the end of @georg's answer, but that won't work for very old browsers, so be aware of that.


Javascript doesn't support lookbehinds, so split is not possible. match works:

str.match(/^(\S+)\s(.*)/).slice(1)

Another trick:

str.replace(/\s+/, '\x01').split('\x01')

how about:

[str.replace(/\s.*/, ''), str.replace(/\S+\s/, '')]

and why not

reverse = function (s) { return s.split('').reverse().join('') }
reverse(str).split(/\s(?=\S+$)/).reverse().map(reverse)

or maybe

re = /^\S+\s|.*/g;
[].concat.call(re.exec(str), re.exec(str))

2019 update: as of ES2018, lookbehinds are supported:

str = "72 tocirah sneab"
s = str.split(/(?<=^\S+)\s/)
console.log(s)

In ES6 you can also

let [first, ...second] = str.split(" ")
second = second.join(" ")

Late to the game, I know but there seems to be a very simple way to do this:

const str = "72 tocirah sneab";
const arr = str.split(/ (.*)/);
console.log(arr);

This will leave arr[0] with "72" and arr[1] with "tocirah sneab". Note that arr[2] will be empty, but you can just ignore it.

For reference:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#Capturing_parentheses