JavaScript RegExp W Metacharacter (original) (raw)

Last Updated : 06 Dec, 2024

The \W metacharacter in JavaScript regular expressions matches any character that is not a word character. A word character is defined as:

Essentially, \W matches anything that is not a letter, digit, or underscore.

JavaScript `

let regex = /\W/g; let str = "Hello, World! 123_456"; let matches = str.match(regex); console.log(matches);

`

Output

[ ',', ' ', '!', ' ' ]

The pattern \W matches the non-word characters: a comma, space, exclamation mark, and another space.

Syntax:

/\W/

Use the g flag to match all non-word characters in the string.

Key Points

Real-World Examples

1. Matching Non-Word Characters

JavaScript `

let regex = /\W/g; let str = "hello_world!123"; let matches = str.match(regex); console.log(matches);

`

Here, the \W metacharacter matches the exclamation mark, which is the only non-word character.

2. Removing Non-Word Characters

JavaScript `

let regex = /\W/g; let str = "Hello, World! 123"; let result = str.replace(regex, ""); console.log(result);

`

Using \W with replace(), we remove all non-word characters, leaving only letters, digits, and underscores.

3. Counting Non-Word Characters

JavaScript `

let regex = /\W/g; let str = "Goodbye, cruel world!"; let count = (str.match(regex) || []).length; console.log(count);

`

The \W metacharacter counts all spaces and punctuation marks in the string.

4. Splitting on Non-Word Characters

JavaScript `

let regex = /\W+/; let str = "split,this.string!by?punctuation"; let parts = str.split(regex); console.log(parts);

`

Output

[ 'split', 'this', 'string', 'by', 'punctuation' ]

The \W+ pattern splits the string into parts based on consecutive non-word characters.

5. Validating a String for Special Characters

JavaScript `

let regex = /\W/; let username = "User_123"; if (regex.test(username)) { console.log("Invalid username. Contains special characters."); } else { console.log("Valid username."); }

`

Here, \W checks if the username contains any characters other than letters, digits, or underscores.

Common Patterns Using \W

str.replace(/\W/g, "");

/[\W_]/g

Matches all non-word characters, including underscores.

str.split(/\W+/);

(str.match(/\W/g) || []).length;

Why Use \W?

Conclusion

The \W metacharacter is a powerful and simple way to handle non-word characters in JavaScript, making it invaluable for text manipulation and input validation tasks.