regular expression to allow spaces between words

Written by:

I want a regular expression that prevents symbols and only allows letters and numbers. This regex works great but it doesn’t allow for spaces between words.


For example, when using this regular expression “HelloWorld” is fine but “Hello World” does not match.

How can I tweak it to allow spaces?


Just add a space in your character class!

^[a-zA-Z0-9_ ]*$


Now, if you really want to be strict…

As commented, due to the fact that * means zero or more, all the following are allowed:

  • An empty string, “”.
  • A string comprised entirely of spaces, ”      “.
  • A string that leads and / or trails with spaces, ”   Hello World  “.
  • A string that contains multiple spaces in between words, “Hello   World”.

Originally I didn’t think such details were worth going into, since OP was asking such a basic question that I guessed strictness wasn’t a concern. Now that the question’s gained some popularity however, allow me to redeem myself through this update.

Use @stema’s answer. (Or at least, upvote his.)

Once you read @stema’s answer, here are two modifications, for completeness:

  • If you want to allow multiple spaces between words (say, if you’d like to allow accidental double-spaces, or if you’re working with copy-pasted text from a PDF), then add a + after the space:
    ^w+( +w+)*$
  • If you want to allow tabs and newlines (whitespace characters), then replace the space with a s+:

    Here I suggest the + by default because, for example, Windows linebreaks consist of two whitespace characters in sequence, rn, so you’ll need the + to catch both.

Still not working?

Check what dialect of regular expressions you’re using.* In languages like Java you’ll have to escape your backslashes, i.e. \w and \s. In older or more basic languages and utilities, like sed, w and s aren’t defined, so write them out with character classes, e.g. [a-zA-Z0-9_] and [fnprt].


* I know this question is tagged , but based on 25,000+ views, I’m guessing it’s not only those folks who are coming across this question. Currently it’s the first hit on google for the search phrase, regular expression space word.

One possibility would be to just add the space into you character class, like acheong87 suggested, this depends on how strict you are on your pattern, because this would also allow a string starting with 5 spaces, or strings consisting only of spaces.

The other possibility is to define a pattern:

I will use w this is in most regex flavours the same than [a-zA-Z0-9_] (in some it is Unicode based)

^w+( w+)*$

This will allow a series of at least one word and the words are divided by spaces.

^ Match the start of the string

w+ Match a series of at least one word character

( w+)* is a group that is repeated 0 or more times. In the group it expects a space followed by a series of at least one word character

$ matches the end of the string

Alan Moore

This one worked for me

([w ]+)


Try with:

^(w+ ?)*$


w             - alias for [a-zA-Z_0-9]
"whitespace"?  - allow whitespace after word, set is as optional


I assume you don’t want leading/trailing space. This means you have to split the regex into “first character”, “stuff in the middle” and “last character”:

^([a-zA-Z0-9_][a-zA-Z0-9_ ]*[a-zA-Z0-9_]$

or if you use a perl-like syntax:

^w[w ]*w$

Also: If you intentionally worded your regex that it also allows empty Strings, you have to make the entire thing optional:

^(w[w ]*w)?$

If you want to only allow single space chars, it looks a bit different:

^((w+ )*w+)?$

This matches 0..n words followed by a single space, plus one word without space. And makes the entire thing optional to allow empty strings.

This does not allow space in the beginning. But allowes spaces in between words. Also allows for special characters between words. A good regex for FirstName and LastName fields.



Had a good look at many of these supposed answers…

…and bupkiss after scouring stackoverflow as well as other sites for a regex that matches any string with no starting or trailing white-space and ONLY A SINGLE SPACE between strictly alpha character words.


thus easily modified to alphanumeric


(this does not match single words but just use a switch/if-else with a simple ^[a-zA-z0-9]+$ if you need to catch single words in adition)

enjoy :D

try .*? to allow white spaces it worked for me


regular expression to allow spaces between words
0 votes, 0.00 avg. rating (0% score)

Leave a Reply