Open In App

JavaScript RegExp \S Metacharacter

Last Updated : 28 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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

JavaScript
let str = "Geeky@128";
let regex = new RegExp("\\S", "g");

let match = str.match(regex);

console.log(match);

Output
[
  'G', 'e', 'e',
  'k', 'y', '@',
  '1', '2', '8'
]

Syntax

/\S/ 
// or
new RegExp("\\S")

Syntax with modifiers

/\S/g 
// or
new RegExp("\\S", "g")

Example 1: Matches the non-whitespace characters. 

JavaScript
let str = "GeeksforGeeks @ _123_ $";
let regex = /\S/g;
let match = str.match(regex);

console.log(match);

Output
[
  'G', 'e', 'e', 'k', 's',
  'f', 'o', 'r', 'G', 'e',
  'e', 'k', 's', '@', '_',
  '1', '2', '3', '_', '$'
]

Example 2: Matches the non-whitespace strings. 

JavaScript
const regex = /\S+/g;
const str = "Hello World!   This is JavaScript";
const matches = str.match(regex);
console.log(matches);

Output
[ 'Hello', 'World!', 'This', 'is', 'JavaScript' ]

Supported Browsers

  • Chrome
  • Safari
  • Firefox
  • Opera
  • Edge

We have a complete list of Javascript RegExp expressions, to check those please go through this JavaScript RegExp Complete Reference article.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.  



Next Article

Similar Reads