Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split a string by whitespace and ignoring leading and trailing whitespace into an array of words using a regular expression?

I typically use the following code in JavaScript to split a string by whitespace.

"The quick brown fox jumps over the lazy dog.".split(/\s+/);
// ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."]

This of course works even when there are multiple whitespace characters between words.

"The  quick brown fox     jumps over the lazy   dog.".split(/\s+/);
// ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."]

The problem is when I have a string that has leading or trailing whitespace in which case the resulting array of strings will include an empty character at the beginning and/or end of the array.

"  The quick brown fox jumps over the lazy dog. ".split(/\s+/);
// ["", "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog.", ""]

It's a trivial task to eliminate such empty characters, but I'd rather take care of this within the regular expression if that's at all possible. Does anybody know what regular expression I could use to accomplish this goal?

like image 911
natlee75 Avatar asked Feb 16 '13 16:02

natlee75


People also ask

How do you split a string by whitespace?

To split a string with space as delimiter in Java, call split() method on the string object, with space " " passed as argument to the split() method. The method returns a String Array with the splits as elements in the array.

How do you split a space in ignore?

To split the sentences by comma, use split(). For removing surrounding spaces, use trim().

How do I split a string in Word?

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

Which of the follow removes leading and trailing whitespace from a string?

newStr = strtrim( str ) removes leading and trailing whitespace characters from str and returns the result as newStr .


3 Answers

If you are more interested in the bits that are not whitespace, you can match the non-whitespace instead of splitting on whitespace.

"  The quick brown fox jumps over the lazy dog. ".match(/\S+/g);

Note that the following returns null:

"   ".match(/\S+/g)

So the best pattern to learn is:

str.match(/\S+/g) || []
like image 76
kennebec Avatar answered Sep 30 '22 07:09

kennebec


" The quick brown fox jumps over the lazy dog. ".trim().split(/\s+/);

like image 45
Josh Avatar answered Sep 30 '22 07:09

Josh


Instead of splitting at whitespace sequences, you could match any non-whitespace sequences:

"  The quick brown fox jumps over the lazy dog. ".match(/\S+/g)
like image 16
Gumbo Avatar answered Sep 30 '22 06:09

Gumbo