RegExp Character Classes
- turned_in_notCharacter Classes
- .
- \w, \W
- \d, \D
- \s, \S
- \ooo, \xhh, \uhhhh, \u{hhhh}
- \n, \r, \t, \v, \f, \0
- \
- To run below examples, try it inside
<script>
tag, in a basic html file
. (dot)
It matches any single character, except line terminators (\n, \r, \u2028, \u2029)
For example :
var str="books are good friend";
var pattern=/.e/g;
alert(str.match(pattern));
// Output: re,ie
Matches: books are good friend
\w
It matches any word character including underscore
same as [a-zA-Z0-9_]
For example :
var str="good food today";
var pattern=/\wood/g;
alert(str.match(pattern));
// Output: good,food
Matches: good food today
\W
It matches any non-word character
same as [^a-zA-Z0-9_]
For example :
var str="90%+ marks!";
var pattern=/\W/g;
alert(str.match(pattern));
// Output: %,+, ,!
Matches: 90%+ marks!
\d
It matches any digit character
same as [0-9]
For example :
var str="got 90% marks!";
var pattern=/\d/g;
alert(str.match(pattern));
// Output: 9,0
Matches: got 90% marks!
\D
It matches any non-digit character
same as [^0-9]
For example :
var str="got 90%";
var pattern=/\D/g;
alert(str.match(pattern));
// Output: g,o,t, ,%
Matches: got 90%
\s
It matches any whitespace character including space, tab, vertical tab, new line, carriage return, form feed and Unicode spaces
same as
[ \t\v\n\r\f\u00a0\u1680\u200a-\u200a\u2028\u2029\u202f\u205f\3000\ufeff]
For example :
var str="got 90% marks!";
var pattern=/.\s./g;
alert(str.match(pattern));
// Output: t 9,% m
Matches: got 90% marks!
\S
It matches any non-whitespace character other than '\s' characters
same as
[^ \t\v\n\r\f\u00a0\u1680\u200a-\u200a\u2028\u2029\u202f\u205f\3000\ufeff]
For example :
var str="got 90%";
var pattern=/\S/g;
alert(str.match(pattern));
// Output: g,o,t,9,0,%
Matches: got 90%
\ooo
It matches octal number ooo (3 digit combination of [0-7])
For example :
var str="great day";
var pattern=/\145/g; // \145 = "e"
alert(str.match(pattern));
// Output: e
Matches: great day
\xhh
It matches 2 digit hexadecimal number hh (2 digit combination of [0-9a-f]), case-insensitive
For example :
var str="great work";
var pattern=/\x67../g; // \x67 = "g"
alert(str.match(pattern));
// Output: gre
Matches: great work
\uhhhh
It matches 4 digit hexadecimal number hhhh (4 digit combination of [0-9a-f]), case-insensitive
For example :
var str="be quick";
var pattern=/\u0071/g; // \u0071 = "q"
alert(str.match(pattern));
// Output: q
Matches: be quick
\u{hhhh}
It matches extended hexadecimal number hhhh or hhhhh (4 or 5 digit combination of [0-9a-f]), case-insensitive & works only when 'u' flag is set
For example :
var str="you go";
var pattern=/\u{0079}/ug; // \u0079 = "y"
alert(str.match(pattern));
// Output: y
Matches: you go
\n,\r,\t,\v,\f,\0
\n : Matches new line character
\r : Matches carriage return character
\t : Matches tab character
\v : Matches vertical tab character
\f : Matches form feed character
\0 : Matches NUL character
For example :
var str="got\n90%";
var pattern=/.\n./g;
alert(str.match(pattern));
// Output: t\n9
\
It's used only with the characters creating special meaning and to be treated as individual character
For example :
/./
: means any single character, except line terminators/\./
: means just a single character '.'(dot)/\d/
: means any digit character/\\d/
: means just single characters '\' & 'd' together
if you use '.'(dot) character, like /./
to search for '.'(dot) in your string then it results other then just searching a '.'(dot). since it has a special meaning. So to avoid special meaning we use '\' just before it
var str="12.5";
var pattern=/./g;
alert(str.match(pattern));
// Output: 1,2,.,5
pattern=/./g;
alert(str.match(pattern));
// Output: .
- Alert! Kindly use '\' with special characters if found special meaning characters, otherwise you may get wrong output. since some special characters are used for special purposes
Hello welcome back, abcd efgh ijkl mnopq rstuv wxyz ABCD EFGH IJKL MNOPQ RSTUV WXYZ 01234 56789 */? +-$#= @!^&| _([{
/abc/g
*Important
If you are using any of the special meaning pattern with '\' before it, like '\w' or '\W' or '\d' and many more, and also using new RegExp("pattern","flags")
syntax for match then use '\' just before the special meaning pattern
For example:
var patt=/\w/g;
var patt1=new RegExp("\\w","g");
//here patt1 is similar to patt, both results the same
// If still having issue run patternVarible.toString(),
// you can learn about it covered under RegExp Methods
alert(patt.toString() == patt1.toString()); // returns true
/*More Examples :
/\w\doo/g can be written as new RegExp("\\w\\doo","g")
/\W\so/ig can be written as new RegExp("\\W\\so","ig")
*/