- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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?
This article discusses about the \w vs \W in JavaScript regex. The \w and \W meta characters are used to match the characters of a given string. \w and \W are different from each other. \w (character) is equivalent to [a-zA-Z0-9_] that means it matches any single letter, number, or underscore.
The characters that are not matched by the \w are matched by the \W. \W is equivalent to [^a-zA-Z0-9_] that means it matches any character other than letter, number and underscore. To match a string with a pattern, the possible methods are: string.match(pattern), string.search(pattern), pattern.exec(string), pattern.test(text). We use match method only in these examples.
Syntax
The syntax for \w regex is −
RegExp("\w", "g") Or /\w/g
Example 1
This is an example program to use the \w metacharacter.
<!DOCTYPE html> <html> <head> <title>\w vs \W in JavaScript regex</title> </head> <body style="text-align : center"> <h3>\w vs \W in JavaScript regex</h3> <p id='result'></p> <script> var string = "!#@ Tutorials point is one of the best e-learning platform @#!" var reg_ex = new RegExp("\w", "g"); var output = string.match(reg_ex); document.getElementById('result').innerHTML = output; </script> </body> </html>
On executing the above code, the following output is generated.
Example 2
The syntax for \W regex is −
RegExp("\W", "g") Or /\W/g
This is an example program to use the \W metacharacter.
<!DOCTYPE html> <html> <head> <title>\w vs \W in JavaScript regex</title> </head> <body style="text-align : center"> <h3>\w vs \W in JavaScript regex</h3> <p id='result'></p> <script> var string = "!#@ Tutorials point is one of the best e-learning platform @#!" var reg_ex = new RegExp("\W", "g"); var output = string.match(reg_ex); document.getElementById('result').innerHTML = output; </script> </body> </html>
On executing the above code, the following output is generated.
Example3
This is an example program to compare the result for \w and \W pattern and user defined patterns.
<!DOCTYPE html> <html> <head> <title>\w vs \W in JavaScript regex</title> </head> <body style="text-align : center"> <h3>\w vs \W in JavaScript regex</h3> <p id='result'></p> <script> var string = "!#@_ Tutorials point is one of the best e-learning platform _@#!" var reg_ex1 = new RegExp("\w", "g"); //Existing Regular expression Metacharacter \w var reg_ex2 = /[a-zA-Z0-9_]/g; // User-defined pattern var reg_ex11 = /\W/g; //Existing Regular expression Metacharacter \W var reg_ex22 = /[^a-zA-Z0-9_]/g; // User-defined pattern var output1 = string.match(reg_ex1); var output2 = string.match(reg_ex2); var output11 = string.match(reg_ex11); var output22 = string.match(reg_ex22); document.getElementById('result').innerHTML = 'For \w :'+'<br/>'+' Using RegExp("\w", "g") : '+output1+'<br/>'+'Using /[a-zA-Z0-9_]/g : '+output2+'<br/>'+'For \W :'+'<br/>'+'Using /\W/g : '+output11+'<br/>'+'Using /[^a-zA-Z0-9_]/g : '+output22; </script> </body> </html>
On executing the above code, the following output is generated.
- Related Articles
- JavaScript RegExp W Metacharacter
- The maximum resistance which can be made using four resistors each of resistance $frac {1}{2}$W is(a) 2 W (b) 1 W (c) 2.5 W (d) 8 W
- An electric bulb is rated $220 V$ and $100 W$. When it is operated on $110 V$, the power consumed will be$(a)$ 100 W$(b)$ 75 W$(c)$ 50 W$(d)$ 25 W
- Regular Expression "W" Metacharacter in Java
- Explain Regular Expression "w" Metacharacter in Java
- W and Z registers in 8085 Microprocessor
- Which uses more energy, a $250 W$ TV set in $1 hr$, or a $1200 W$ toaster in $10 minutes$?
- Two bulbs of 100 W and 40 W are connected in series. The current through the 100 W bulb is 1 A. The current through the 40 W bulb will be :(a) 0.4 A (b) 0.6 A (c) 0.8 A (d) 1 A
- If the continued fraction form of ( frac{97}{19} ) is ( w+frac{1}{x+frac{1}{y}} ) where ( w, x, y ) are integers, then find the value of ( w+x+y ).
- Find the following product.( left(frac{4}{3} u^{2} v wright) timesleft(-5 u v w^{2}right) timesleft(frac{1}{3} v^{2} w uright) )
- Difference b/w getText() and getAttribute() in Selenium WebDriver
- 8 Principles of Enterprise W ide Business Process Management
- Construct a Turing Machine for language L = {ww | w ∈ {0,1}}
- Construct a TM for the language L= {ww : w ∈ {0,1}}
- Construct a Turing Machine for language L = {wwr | w ∈ {0, 1}}
