if word contains space detects using jquery 1

if word contains space detects using jquery

What you have will find a space anywhere in the string, not just between words.

If you want to find any kind of whitespace, you can use this, which uses a regular expression:

if (/\s/.test(str)) {
    // It has any kind of whitespace
}

\s means "any whitespace character" (spaces, tabs, vertical tabs, formfeeds, line breaks, etc.), and will find that character anywhere in the string.

Here is what the above code is Doing:
1. /\s/ is a regular expression that means “any whitespace character”.
2. test() is a method of the regular expression object that returns true if the string contains any match to the regular expression.
3. So, /\s/.test(str) returns true if str contains any whitespace.

Similar Posts