Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
QuakeLive

Check If A String Contains No Spaces

Recommended Posts

Quote [page 413 - Character Classes... ]:

 

 

3. Check if a string contains no spaces. The \S character class shortcut will match non-white space characters. To make sure that the entire string contains no spaces, use the caret and the dollar sign: ^\S$. If you don’t use those, then all the pattern is confirming is that the subject contains at least one non-space character.

 

But, ^\S$ does not check if a string contains no spaces, it only checks if a string is a non-white space character, right?

 

2rrw41z.jpg

Share this post


Link to post
Share on other sites

Yes, that is correct.

 

One thing the quote above does not clarify is whether we are talking about a "space" or any whitespace character. \s refers to any whitespace character, not just a space.

 

A better way to check if a string contains one or more spaces is:

str.indexOf(' ') !== -1;

That's the most efficient way. You can also use the following regex if you want, although it's not necessary in this case:

/ /.test(str);

You can also modify the regex above to check for any whitespace characters:

/\s/.test(str);

That answer your concerns?

  • Upvote 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...