Open In App

JavaScript RegExp \W Metacharacter

Improve
Improve
Like Article
Like
Save
Share
Report

The RegExp \W Metacharacter in JavaScript is used to find the nonword character i.e. characters which are not from a to z, A to Z, 0 to 9. It is the same as [^a-zA-Z0-9]. 

Syntax: 

/\W/ 

or

new RegExp("\\W")

Syntax with modifiers:

/\W/g 

or

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

Example 1: This example searches the non words characters. 

Javascript




function geek() {
    let str1 = "GeeksforGeeks@_123_$";
    let regex4 = /\W/g;
    let match4 = str1.match(regex4);
 
    console.log("Found " + match4.length
        + " matches: " + match4);
}
geek()


Output

Found 2 matches: @,$

 Example 2: This example searches the non-word characters in the whole string. 

Javascript




function geek() {
    let str1 = "Geeky@128";
    let regex4 = new RegExp("\\W", "g");
    let match4 = str1.match(regex4);
 
    console.log("Found " + match4.length
        + " matches: " + match4);
}
geek()


Output

Found 1 matches: @

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

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

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.  


Last Updated : 30 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads