

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
w vs W in JavaScript regex?
\w vs \W
There is a lot of variation between '\w' and '\W' in javascript in which the former looks after 'word characters' such as alpha-numerics whereas the latter looks after 'non-word characters' such as &, ^, %, etc. Let's discuss it in a nutshell.
syntax-1
new RegExp("\\w", "g");
The above code gives out the syntax to find the 'word characters' in javascript.
syntax-2
new RegExp("\\W", "g");
The above code gives out the syntax to find the 'non-word characters' in javascript.
Example-1
In the following example, \w along with global object 'g' is used. If global object 'g' is not used then, only the first alphanumeric letter, if present, will be displayed in the output. Since here global object 'g' is used, all the alpha-numeric characters are displayed as shown in the output.
<html> <body> <script> var str = "**Tutorix is the best e-learning platform%!"; var regpat = /\w/g; var result = str.match(regpat); document.write(result); </script> </body> </html>
Output
T,u,t,o,r,i,x,i,s,t,h,e,b,e,s,t,e,l,e,a,r,n,i,n,g,p,l,a,t,f,o,r,m
Example-2
In the following example, '\W' is used therefore the 'non-word characters' are displayed in the output. If the global object 'g' is not used then only the first "non-word character" will be displayed. Since here "g" is used all the non-word characters are displayed as shown in the output.
<html> <body> <script> var str = "**Tutorix is the best e-learning platform%!"; var regpat = /\W/g; var result = str.match(regpat); document.write(result); </script> </body> </html>
Output
*,*, , , , ,-, ,%,!
- Related Questions & Answers
- \w vs \W in JavaScript regex?
- JavaScript RegExp \W Metacharacter
- W and Z registers in 8085 Microprocessor
- Regular Expression "\W" Metacharacter in Java
- Explain Regular Expression "\w" Metacharacter in Java
- Difference b/w getText() and getAttribute() in Selenium WebDriver?
- Construct a Turing Machine for language L = {ww | w ∈ {0,1}}
- Construct a TM for the language L= {ww : w ∈ {0,1}}
- Python program to wrap a text into paragraph with width w
- Construct a Turing Machine for language L = {wwr | w ∈ {0, 1}}
- Design a push down automaton for L = {wwR | w ∈ {a, b}+}?
- C++ code to find out if an image is B/W or color
- Find the Number of Paths of Weight W in a K-ary tree using C++
- C program for DFA accepting all strings over w ∈(a,b)* containing “aba” as a substring
- Replace commas with JavaScript Regex?
- Match specific word in regex in JavaScript?