With JavaScript Regular Expression find a non-whitespace character.

To find a non-whitespace character in JavaScript regular expressions, use the \S metacharacter. This matches any character that is not a space, tab, newline, or other whitespace character.

Syntax

\S    // Matches any non-whitespace character
\S+   // Matches one or more non-whitespace characters
\S*   // Matches zero or more non-whitespace characters

Example: Finding Non-Whitespace Characters

<html>
   <head>
      <title>JavaScript Regular Expression</title>
   </head>
   <body>
      <script>
         var myStr = "100% Responsive!";
         var reg = /\S/g;
         var match = myStr.match(reg);
         
         document.write("Original string: " + myStr + "<br>");
         document.write("Non-whitespace characters: " + match.join(""));
      </script>
   </body>
</html>

Output

Original string: 100% Responsive!
Non-whitespace characters: 100%Responsive!

Extracting Words Using \S+

To find complete words (sequences of non-whitespace characters), use \S+:

<html>
   <head>
      <title>Extract Words</title>
   </head>
   <body>
      <script>
         var text = "Hello World 123!";
         var wordPattern = /\S+/g;
         var words = text.match(wordPattern);
         
         document.write("Text: " + text + "<br>");
         document.write("Words found: " + words.join(", "));
      </script>
   </body>
</html>

Output

Text: Hello World 123!
Words found: Hello, World, 123!

Comparison with Whitespace Pattern

Pattern Matches Example
\S Any non-whitespace character a, 1, %, @
\s Any whitespace character space, tab, newline

Conclusion

The \S metacharacter is essential for matching non-whitespace characters in JavaScript regular expressions. Use \S+ to extract complete words or tokens from strings.

Updated on: 2026-03-15T23:18:59+05:30

506 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements