Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Search from all Lessons


LoginGet Started
← Back to How to's
  • Java

  • javascript

  • python

  • regular-expression

Edit on Github

RegEx for whitespaces

Written by:

Regular expressions combine both basic and special characters to detect patterns in strings, some of those patterns include whitespaces.

There are several ways on regular expressions to detect whitespaces, the most used one would be the \s or \s+ since they detect not just whitespaces made by the space bar, but with tab and return key (line jump).

1//Has white spaces 2const text = "4 Geeks Academy" 3const hasSpaces = (str) =>{ 4 const regex = new RegExp("\s+") 5 return regex.test(str) 6} 7 8console.log(hasSpaces(text)) 9 10// Output -> true

Regular Expression (RegEx) for whitespaces

Dealing with spaces is a common action when receiving data from our apps, that's why we need to learn how to detect them. Using RegEx allows us to easily detect the presence of white spaces and deal with them as needed.

We can use different RegEx to achieve this:

  • \s Will take a single whitespace character, but when dealing with line jumps and text separated by the "return" button, won't return anything, since en the exposed cases, are more than one whitespace.
  • \s+ When added the +, you are stating that can be one, or any amount of whitespaces (spaces, tabs, and line breaks).
  • [\r\n\t\f\v ] This RegEx is equivalent to \s but we add it for ompatibility reasons with older programming languages.
  • \t matches tabular whitespaces (" "). The \s+ will match this as well.
  • \w matches alphanumeric characters and the underscore (whitespaces included)

Usually, you'll find the \s and \s+ are the most used ones, since they comprehend the other ones we discussed.

Implementation:

In the following example, we'll be checking if a url has whitespaces or not.

1const badURL = "https://4geeks.com/how to"; 2const goodURL = "https://4geeks.com/how-to" 3 4 5const checkURL = (url) =>{ 6 const regex = /\s/; 7 if (regex.test(url)) { 8 return (`Testing '${url}': Whitespace found in the URL.`); 9 } else { 10 return (`Testing '${url}': No whitespace found in the URL.`); 11 } 12} 13 14checkURL (badURL) 15// Testing 'https://4geeks.com/how to': Whitespace found in the URL. 16 17checkURL (goodURL) 18// Testing 'https://4geeks.com/how-to': No whitespace found in the URL.

Python version:

1import re 2 3#whitespaces 4badURL = "https://4geeks.com/how to"; 5goodURL = "https://4geeks.com/how-to" 6 7def checkURL(url): 8 regex = r"/\s/" 9 match = bool(re.search(regex, url)) 10 if (match == True): 11 return (f"Testing '{url}': Whitespace found in the URL.") 12 else: 13 return (f"Testing '{url}':No whitespace found in the URL.") 14 15print(checkURL(barURL)) 16# Testing 'https://4geeks.com/how to': Whitespace found in the URL. 17print(checkURL(goodURL)) 18# Testing 'https://4geeks.com/how-to': No whitespace found in the URL.

A regular expression (also called regex or regexp) is a way to describe a pattern. It is used to locate or validate specific strings or patterns of text in a sentence, document, or any other character input. You can learn more about Regular Expressions in this RegEx Tutorial.