RegExp character class digit (\d) in Javascript
The RegExp character class digit (\d) used to match any digit (Arabic numeral) in the given string. It is equivalent to [0-9] a range square bracket. ie. /\d/ or /[0-9]/ matches ]10' in 'border-size: 10px.
character class digit (\d)
 1 const regex = new RegExp(/\d/g);
 2 
 3 const str1 = 'border: 10px;';
 4 const str2 = '10sec';
 5 const str3 = 'years';
 6 
 7 // validate using test()
 8 let res1 = regex.test(str1);
 9 regex.lastIndex = 0; // reset regex to starts from index 0
 10 let res2 = regex.test(str2);
 11 regex.lastIndex = 0; // reset regex to starts from index 0
 12 let res3 = regex.test(str3);
 13 
 14 console.log('A string includes digits :', res1);
 15 console.log('A string includes digits :', res2);
 16 console.log('A string includes digits :', res3);
In the above example, a regex method called by specifying character class digit (\d) with global flag. A string is validating with test() methods of RegExp which returns boolean value true, if string includes any digits, otherwise false.
Output
 1 A string includes digits : true
 2 A string includes digits : true
 3 A string includes digits : false

digit class to extract matches

digit class to extract matches
 1 const regex = new RegExp(/\d/g);
 2 
 3 const str1 = 'margin: 10px;';
 4 const str2 = 'year 15';
 5 const str3 = 'Javascript';
 6 
 7 // validate using match()
 8 let res1 = str1.match(regex);
 9 let res2 = str2.match(regex);
 10 let res3 = str3.match(regex);
 11 
 12 console.log('Matches in string :', res1);
 13 console.log('Matches in string :', res2);
 14 console.log('Matches in string :', res3);
In the above example, a string defines that initialized with string literals. A match() method called by specifying a regex that returns an array which includes matches, if presents in the string, otherwise null. A results assigned to the variable and print.
Output
 1 Matches in string : [ '1', '0' ]
 2 Matches in string : [ '1', '5' ]
 3 Matches in string : null

digit class with exec() method

digit class with exec() method
 1 const regex = new RegExp(/\d/g);
 2 
 3 const str1 = 'margin: 10px;';
 4 const str2 = 'year 15';
 5 const str3 = 'Javascript';
 6 
 7 // validate using exec()
 8 let res1 = regex.exec(str1);
 9 regex.lastIndex = 0;
 10 let res2 = regex.exec(str2);
 11 regex.lastIndex = 0;
 12 let res3 = regex.exec(str3);
 13 
 14 console.log('Matches in string :', res1);
 15 console.log('Matches in string :', res2);
 16 console.log('Matches in string :', res3);
In the above example, a string defines that initialized with string literals. A exec() method of regex called by specifying a string that returns an array which includes first match, if presents in the string, otherwise null. A results assigned to the variable and print.
Output
 1 Matches in string : [ '1', index: 8, input: 'margin: 10px;', groups: undefined ]
 2 Matches in string : [ '1', index: 5, input: 'year 15', groups: undefined ]
 3 Matches in string : null
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us