Demystifying test() of RegEx
using this more makes me hate Regex less...
I know, I know. Nobody likes Regex, but this small method that can be applied to it will make you significantly less repulsed when you see it. And trust me, I know all about repulsion from Regex.
In JavaScript, the RegExp
object provides a set of built-in methods for working with regular expressions. One of these methods is test()
, which is used to test a string for a match against a regular expression. The test()
method returns a boolean value indicating whether or not the given string matches the regular expression.
const regex = /[a-z]/;
const str = "Namaste!";
const hasLowercase = regex.test(str);
console.log(hasLowercase); // true
The regular expression [a-z]
matches any lowercase letter in the string str
. The test()
method returns true
because the string contains at least one lowercase letter.
You can use it for entire words as well:
const regex = /cat/;
const str1 = "The cat is crazy";
const str2 = "The dog is done";
console.log(regex.test(str1)); // true
console.log(regex.test(str2)); // false
The common syntax of the test()
method is:
regex.test(str)
Here regex
is the regular expression to match against, and str
is the string to test.
Other ways
Simple Regular Expression
const regex = /namaste/;
const str = 'namaste universe';
const result = regex.test(str);
console.log(result); // true
Regular Expression with the 'i' flag
const regex = /albus/i;
const str = 'Albus was here.';
const result = regex.test(str);
console.log(result); // true
If we add the i
flag to the regular expression to make it case-insensitive. We then use the test()
method to check if the string 'Albus was here.' contains the word 'albus' (case-insensitive). Returns true.
Regular Expression with a Character Set
const regex = /[aeiou]/;
const str = 'Namaste';
const result = regex.test(str);
console.log(result); // true
If we create a regular expression that matches any vowel ('a', 'e', 'i', 'o', or 'u') using a character set. We then use the test()
method to check if the string 'Namaste' contains any vowels. Returns true.
Regular Expression with a Quantifier
const regex = /a+/;
const str = 'namaste';
const result = regex.test(str);
console.log(result); // true
If we create a regular expression that matches one or more occurrences of the letter 'a'. We then use the test()
method to check if the string 'namaste' contains one or more occurrences of the letter 'a'. The method returns true
since the string contains the letter 'a' multiple times.
Conclusion
Knowing the test()
method in JavaScript is important for several reasons:
Pattern matching: Regular expressions are used for pattern matching in strings. The
test()
method helps to verify if a string matches a specific pattern.Validation: Often, we need to validate user input on web forms. The
test()
method can be used to check if the user's input follows a certain pattern (e.g. valid email address format).Searching: The
test()
method can also be used to search for patterns in a string. If the method returns true, it means the pattern exists in the string.Efficiency: The
test()
method is an efficient way to check if a string matches a specific pattern. It returns a boolean value instead of the location of the match (as with other methods likematch()
), which can be more useful in certain scenarios.
Useful Post Script
How to check if a string contains only alphanumeric characters:
const str = "AbCd1234";
const regex = /^[a-zA-Z0-9]+$/;
const isValid = regex.test(str);
console.log(isValid); // Output: true
Hope this helps you as much as it did me.