Andrew Andrew - 8 months ago 46
Javascript Question

Split a string into an array of words, punctuation and spaces in JavaScript

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.

Answer Source

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 anything other than whitespace and word character.

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


Regex explanation here

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