Shorthand Character Classes make the use of character sets much easier. For digits we use character set [0-9] but shorthand character class \d can be used instead. Thus \d is shorthand character class for [0-9].


RegEx (Regular Expression) Shorthand Character Classes In JavaScript - 6


RegEx Shorthand Character Classes

The following shorthand character classes are used in RegEx:


1. Word Character - \w, \w+, \W, \W+

The word character is used for match underscore, alphabet and numbers. 

\w (Word Character)   

This word character (\w) only match one character.

let regex = /codex\w/;  //  only match one character 

let string = "codexradar";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of  "codexr"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "codexradar" matches the expression "codex\w"


\w+ (Word Character)   

This word character (\w+)  match one or more character.

let regex = /codex\w+/; // match one or more character

let string = "codexradar";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of  "codexradar"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

} code-box


Output : The string "codexradar" matches the expression "codex\w+"


\W (Non Word Character)   

This word character (\W)  match only one non-word character like $@#%^!.

let regex = /codex\W/; // match only one non-word character

let string = "codex$#%radar";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of  "codex$"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "codex$#%radar" matches the expression "codex\W"


\W+ (Non Word Character)   

This word character (\W+)  match one or more  non-word character like $@#%^!.

let regex = /codex\W+/; // match one or more non-word character

let string = "codex$#%radar";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of  "codex$#%"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "codex$#%radar" matches the expression "codex\W+"


2. Digit - \d, \d+, \D, \D+

\d (Digit)   

This digit shorthand character class (\d)  match only one digit.

let regex = /codex\d/; // match only one digit

let string = "codex1234";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of  "codex1"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "codex1234" matches the expression "codex\d"


\d+ (Digit)   

This digit shorthand character class (\d+)  match one or more digit.

let regex = /codex\d+/; // match one or more digit

let string = "codex1234";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of  "codex1234"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "codex1234" matches the expression "codex\d+"


\D (Non Digit)   

This digit shorthand character class (\D)  match only one non digit.

let regex = /\D1234/; // match only one non digit

let string = "codex1234";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of  "x1234"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "codex1234" matches the expression "\D1234"


\D+ (Non Digit)   

This digit shorthand character class (\D+)  match one or more non digit.

let regex = /\D+1234/; // match one or more non digit

let string = "codex1234";

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of  "codex1234"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "codex1234" matches the expression "\D+1234"


3. Whitespace - \s, \s+, \S, \S+

\s (Whitespace)   

This whitespace shorthand character class (\s)  match only one whitespace.

let regex = /codex\s/; // match only one whitespace

let string = "codex radar"; // one white space between these words

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of  "codex "

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "codex radar" matches the expression "codex\s"


\s+ (Whitespace)   

This whitespace shorthand character class (\s+)  match one or more whitespace.

let regex = /codex\s+/; // match one or more whitespace

let string = "codex   radar"; // three white space between these words

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of  "codex   "

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "codex   radar" matches the expression "codex\s+"


\S (Non Whitespace)   

This whitespace shorthand character class (\S)  match only one non whitespace.

let regex = /codex\S/; // match only one non whitespace

let string = "codexradar"; // no white space between these words

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of  "codexr"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "codexradar" matches the expression "codex\S"


\S+ (Non Whitespace)   

This whitespace shorthand character class (\S+)  match one or more non whitespace.

let regex = /codex\S+/; // match one or more non whitespace

let string = "codexradar"; // no white space between these words

let result = regex.exec(string);

if(regex.test(string)) {

console.log(result); // give array of  "codexradar"

console.log(`The string "${string}" matches the expression "${regex.source}"`);

}else {

console.log(`The string "${string}" does not match the expression "${regex.source}"`);

code-box


Output : The string "codexradar" matches the expression "codex\S+"


Summary

In this post, we learned about regular expressions shorthand character classes in JavaScript. Now you practice this. Keep visiting the blog to read more new similar posts.

Post a Comment

Previous Post Next Post