The Wayback Machine - https://web.archive.org/web/20211124222837/https://www.geeksforgeeks.org/javascript-regexp-s-metacharacter/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript | RegExp \s Metacharacter

  • Difficulty Level : Medium
  • Last Updated : 06 Apr, 2021

The RegExp \s Metacharacter in JavaScript is used to find the whitespace characters. The whitespace character can be a space/tab/new line/vertical character. It is same as [ \t\n\r].
Syntax: 
 

/\s/ 

or 
 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

new RegExp("\\s")

Syntax with modifiers: 
 

/\s/g 

or 
 



new RegExp("\\s", "g")

Example 1: This example replaces the white spaces with dash.
 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        JavaScript RegExp \s Metacharacter
    </title>
</head>
 
<body style="text-align:center">
     
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
     
    <h2>RegExp \s Metacharacter</h2>
     
     
<p>
        Input String: GeeksforGeeks is the computer
        science\nportal\tfor geeks.
    </p>
 
     
    <button onclick="geek()">
        Click it!
    </button>
     
    <p id="app"></p>
 
     
    <script>
        function geek() {
            var regex = /\s/g;
            var replace = "-";
            var str = "GeeksforGeeks is the computer "
                    + "science\nportal\tfor geeks.";
            var str1 = str.replace(regex, replace);
            alert("The new string is: \n" + str1);
        }
    </script>
</body>
 
</html>                   

Output: 
Before Clicking the button: 
 

backs

After Clicking the button: 
 

backs

Example 2: This example replaces all the white spaces with hash(#).
 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        JavaScript RegExp \s Metacharacter
    </title>
</head>
 
<body style="text-align:center">
     
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
     
    <h2>RegExp \s Metacharacter</h2>
     
     
<p>
        Input String: Geeks Classes is a Classroom
        programme by GeeksforGeeks to enhance DSA.
    </p>
 
     
    <button onclick="geek()">
        Click it!
    </button>
     
    <p id="app"></p>
 
     
    <script>
        function geek() {
            var regex = new RegExp("\\s", "g");
            var replace = "#";
            var str = "Geeks Classes is a Classroom "
                    + "programme\nby GeeksforGeeks\tto"
                    + " enhance DSA";
            var str1 = str.replace(regex, replace);
            alert("The new string is: \n" + str1);
        }
    </script>
</body>
 
</html>                   

Output: 
Before Clicking the button: 
 

backs

After Clicking the button: 
 

backs

Supported Browsers: The browsers supported by RegExp \s Metacharacter are listed below: 
 

  • Google Chrome
  • Apple Safari
  • Mozilla Firefox
  • Opera
  • Internet Explorer

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :