7

I have a string which I'd like to split into items contained in an array as the following example:

var text = "I like grumpy cats. Do you?"

// to result in:

var wordArray = ["I", " ", "like", " ", "grumpy", " ", "cats", ".", "  ", "Do", " ", "you", "?" ]

I've tried the following expression (and a similar varieties without success

var wordArray = text.split(/(\S+|\W)/)
//this disregards spaces and doesn't separate punctuation from words

In Ruby there's a Regex operator (\b) that splits at any word boundary preserving spaces and punctuation but I can't find a similar for Java Script. Would appreciate your help.

3 Answers 3

10

Use String#match method with regex /\w+|\s+|[^\s\w]+/g.

  1. \w+ - for any word match
  2. \s+ - for whitespace
  3. [^\s\w]+ - for matching combination of anything other than whitespace and word character.

var text = "I like grumpy cats. Do you?";

console.log(
  text.match(/\w+|\s+|[^\s\w]+/g)
)

Regex explanation here


FYI : If you just want to match single special char then you can use \W or . instead of [^\s\w]+.

Sign up to request clarification or add additional context in comments.

1 Comment

Since \s+ has already matched spaces, there are none to ignore so the last term can be just \W+ instead of [^\s\w]+. ;-)
4

The word boundary \b should work fine.

Example

"I like grumpy cats. Do you?".split(/\b/)
// ["I", " ", "like", " ", "grumpy", " ", "cats", ". ", "Do", " ", "you", "?"]

Edit

To handle the case of ., we can split it on [.\s] as well

Example

"I like grumpy cats. Do you?".split(/(?=[.\s]|\b)/)
// ["I", " ", "like", " ", "grumpy", " ", "cats", ".", " ", "Do", " ", "you", "?"]
  • (?=[.\s] Positive look ahead, splits just before . or \s

8 Comments

Damn I really need to sleep. Sorry about that and thanks!
Works except for ".", " "
". " ==> ".", " "
it does works for "." and " " although when they are succeeded by spaces they get appended in the same array item. It's good enough for what I want anyway. Thanks for pointing it out
@PranavCBalan Corrected.
|
0
var text = "I like grumpy cats. Do you?"
var arr = text.split(/\s|\b/);
alert(arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.