JavaScript | RegExp \s Metacharacter

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



new RegExp("\\s")

Syntax with modifiers:

/\s/g 

or

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:
Before Clicking the button:
backs
After Clicking the button:
backs

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


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


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.