If you want to allow tabs and newlines (whitespace characters), then replace the space with a \s+: ^\w+(\s+\w+)*$ 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+)*$ Some things to note about this (and answer: Which, in my flavor (without using \w) translates to: ^+( +)*$ Now that the question's gained some popularity however, I want to say.use answer. Originally I didn't think such details were worth going into, as OP was asking such a basic question that it seemed strictness wasn't a concern. A string that contains multiple spaces in between words, "Hello World".A string that leads and / or trails with spaces, " Hello World ".A string comprised entirely of spaces, " ".Due to the fact that * means zero or more, it would match all of the following cases that one would not usually mean to match: Just add a space in your character class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |