Case and Negation in JavaScript Regular Expressions - Abbreviations for Common Character Classes
(Page 2 of 5 )
d
d stands for any digit, and it abbreviates [0-9]. The following code produces a match:
/IDdid/.test("ID5id is an ID")
Negated d
D is negated d. It represents any character that is not a digit; that is, [^0-9].
s
trnf are white space characters. ' ' or simply ' ' is produced when you press the spacebar of your keyboard. t is produced when you press the tab key on your keyboard. r is the carriage return character. n is the new line character and f is the form feed character.
s is the abbreviation for any white space character. That is s is equivalent to [ trnf].
The following code produces a match:
var alertMsg = "The first line.rnThe second line."
/n/.test(alertMsg)
The following code also produces a match:
/s/.test(alertMsg)
Negated s
S
S is negated s. It represents any character that is not a white space, that is [^s].
S, [^s] and [^ trnf] are equivalent.
The negation symbol negates the class within the square brackets.
w
This is a word character. It represents any alphanumeric character, including the underscore. w and [0-9a-zA-Z_] are equivalent.
Negated w
W is negated w. It represents any non-word character. W and [^w] are equivalent.
The Period '.'
The period '.' matches any character except n. For example, /.s/ matches 'is' in the available string "An apple is on the tree." /.s/ represents two characters, that is any character (except n) followed by 's'.
The dswDSW and dot abbreviations can be used both inside and outside of character classes. Here are some in use:
/dd:dd:dd/ : matches an hh:mm:ss time format, e.g 05:45:30.
/[ds]/ : matches any digit or any white space character.
/wWw/ : matches a word char, followed by a non-word char, followed by a word char.
/..rt/ : matches any two chars, followed by 'rt'
/end./ : matches 'end.'
/end[.]/ : also matches 'end.'
Next: Beginning and Ending a String >>
More JavaScript Articles
More By Chrysanthus Forcha