Episodes
Saturday Nov 11, 2006
Javascript RegExp Predifined classes
Saturday Nov 11, 2006
Saturday Nov 11, 2006
Predefined Classes
Javascript has a set of predefined character classes for you to specify some complex classes. The following table lists all the predefined classes:
Code |
Equal To |
Matches |
---|---|---|
. |
[^ ] |
Any character except new line and carriage return |
d |
[0-9] |
A digit |
D |
[^0-9] |
A non-digit |
s |
[ x0Bf ] |
A white-space character |
S |
[^ x0Bf ] |
A non-white–space character |
w |
[a-zA-Z_0-9] |
A word character (all letters, all numbers, and an underscore) |
W |
[^a-zA-Z_0-9] |
A non-word character |
Suppose you want to match three numbers, without using d. Your code looks like this:
var sToMatch = "567 9838 abc";var reThreeNums = /[0-9][0-9][0-9]/;
alert(reThreeNums.test(sToMatch)); //outputs "true"
Using d, the regular expression becomes much cleaner:
var sToMatch = "567 9838 abc";var reThreeNums = /ddd/;
alert(reThreeNums.test(sToMatch)); //outputs "true"
Version: 20240320
Comments (0)
To leave or reply to comments, please download free Podbean or
No Comments
To leave or reply to comments,
please download free Podbean App.