Check if String contains only Spaces #
To check if a string contains only spaces, call the trim() method on the string and check if the length of the result is equal to 0. If the string has a length of 0 after calling the trim method, then the string contains only spaces.
Copied!
function onlySpaces(str) { return str.trim().length === 0; } console.log(onlySpaces(' ')); // ποΈ true console.log(onlySpaces(' 123 ')); // ποΈ false console.log(onlySpaces('hello ')); // ποΈ false console.log(onlySpaces('')); // ποΈ trueThe String.trim method removes the whitespace characters from both ends of a string.
We check if the result of calling the method on the string returns a string of length 0. If it does, we either have a string that contains only spaces or an empty string.
The trim method does not change the original string, it returns a new string. Strings are immutable in JavaScript.
An alternative, but also very common approach is to use a regular expression.
Check if String contains only with Regular Expression #
To check if a string contains only spaces, use the test() method with the following regular expression /^\s*$/. The test method will return true if the string contains only spaces and false otherwise.
Copied!
function onlySpaces(str) { return /^\s*$/.test(str); } console.log(onlySpaces(' ')); // ποΈ true console.log(onlySpaces(' test ')); // ποΈ false console.log(onlySpaces('hello ')); // ποΈ false console.log(onlySpaces('')); // ποΈ trueWe used the RegExp.test method to check if a string contains only spaces.
The method returns true if the regular expression is matched in the string and false otherwise.
The forward slashes / / mark the beginning and end of the regular expression.
The caret ^ matches the beginning of the input and the dollar sign $ matches the end of the input.
The \s character matches a single space, tab or newline.
The asterisk * character matches the preceding item (space) 0 or more times.
If you ever need help reading a regular expression, bookmark this regex cheatsheet from MDN.
Which approach you pick is a matter of personal preference. I'd go with the trim() method as I find it easier to read and more beginner friendly.
Further Reading #
- Check if String contains Whitespace in JavaScript
- Check if String contains only Letters in JavaScript
- Check if String contains only Latin Letters in JavaScript
- Check if String contains only Digits in JavaScript
- Check if String contains only Letters and Numbers in JS
- Check if String contains only Letters and Spaces in JS
- Ignore case of startsWith, endsWith Methods in JavaScript
PythonServer Side ProgrammingProgramming
Beyond Basic Programming - Intermediate Python
Most Popular
36 Lectures 3 hours
Mohammad Nauman
More Detail
Practical Machine Learning using Python
Best Seller
91 Lectures 23.5 hours
MANAS DASGUPTA
More Detail
Practical Data Science using Python
22 Lectures 6 hours
MANAS DASGUPTA
More Detail
The string can be checked by checking for occurrences of only whitespace characters. We can check if a string contains only whitespace characters using 2 methods. First is using method isspace().
example
print('Hello world'.isspace()) print(' '.isspace())Output
False TrueYou can also use regexes for the same result. For matching only whitespace, we can call the re.match(regex, string) using the regex metacharacter \s as follows: "^\s*$".
example
import re print(bool(re.match('^\s+$', ' abc'))) print(bool(re.match('^\s+$', ' ')))Output
False True
Manogna
Updated on 13-Feb-2020 09:59:53
- Related Questions & Answers
- How to strip spaces/tabs/newlines using Python regular expression?
- How to remove tabs and newlines using Python regular expression?
- Python - Check if dictionary is empty
- How to check if a list is empty in Python?
- How to check if a text field is empty or not in swift?
- How to expand tabs in string to multiple spaces in Python?
- How to check if android editText is empty?
- Python Pandas - Check if an interval is empty
- How to check if String is empty in Java?
- How to check if PSCustomObject is empty in PowerShell?
- How do i match just spaces and newlines with Python regex?
- How can I replace newlines with spaces in JavaScript?
- How to check if a C# list is empty?
- How to check if a Laravel collection is empty?
- Check if value is empty in JavaScript
Previous Page Print Page Next Page
Advertisements