On this temporary tutorial on JavaScript regex matching, you’ll learn find out how to test whether a string matches a daily expression using the test() method.
Strings are pieces of text that may contain quite a lot of data — resembling URLs, phone numbers, names, numbers, and more. In lots of cases, you should check whether or not a string comprises a bit of text or certain kinds of characters.
If you’re testing if a string comprises a particular substring, you is perhaps inclined to make use of a technique like indexOf(). Nonetheless, for more flexible testing and conditions, using regular expressions is a greater option.
JavaScript regex matching permits you to check if a string comprises a particular pattern, substring, or kinds of characters. Regular expressions are useful for detecting information in a string that may be written in numerous formats, resembling dates.
Testing Strings against Regular Expressions
To check whether a string matches a daily expression, you need to first create a daily expression instance. Then, you need to use the test() method available on the regular expression to ascertain if the string matches the regular expression or not.
The test() method accepts one parameter: the string to check against the pattern. It returns a Boolean value indicating whether the string matches the regular expression or not.
For instance:
const pattern = /test.*regular/;
const str = ‘I need to check this string against a daily expression’;
if (pattern.test(str)) {
console.log(‘Matched’);
} else {
console.log(‘Not Matched’);
}
In this instance, you create the pattern test.*regular. This pattern signifies that a string must contain the words test and regular in that order, and that these words may be separated by zero or more occurrences of any character.
If test() returns true, Matched is logged within the console. Otherwise, Not Matched is logged within the console.
Since str comprises the words test and regular, and test precedes regular within the string, it’s going to match against the pattern and test() will return true.
You can too use the RegExp constructor to declare the patterns:
const pattern = latest RegExp(‘test.*regular’);
const str = ‘I need to check this string against a daily expression’;
if (pattern.test(str)) {
console.log(‘Matched’);
} else {
console.log(‘Not Matched’);
}
You may test this out in the next CodePen demo.
See the Pen
Testing a String Against a Regular Expression by SitePoint (@SitePoint)
on CodePen.
Common Examples
This section shows some examples of find out how to use JavaScript regex matching to check common use cases. It needs to be noted that the regular expressions here may not be the proper solution in each case. They’re each used to provide an easy example of how the method works.
Testing URLs
You may test if a string is a URL using regular expressions. You may experiment with this using the next CodePen demo.
See the Pen
Test if a String is a URL in JavaScript by SitePoint (@SitePoint)
on CodePen.
Please note that the regular expression pattern used above expects the URL to start with http:// or https://.
Testing Emails
You may test if a string is a sound email address using regular expressions. The next CodePen demo shows how.
See the Pen
Test is a String is an Email in JS by SitePoint (@SitePoint)
on CodePen.
Testing Dates
You may test if a string is a date using regular expressions. The next CodePen demo shows how it could actually be done.
See the Pen
Test if a String is a date in JavaScript by SitePoint (@SitePoint)
on CodePen.
Please note that the regular expression pattern used above expects the date to be of the formats “DD-MM-YYYY” or “DD/MM/YYYY”.
Other Methods for JavaScript Regex Matching
There are other methods to check whether a string matches a daily expression. This text doesn’t cover all of them, but here’s a temporary overview of them:
- match. This method is offered on strings. It accepts a daily expression as a parameter and retrieves the parts of the string that match the regular expression, if there are any.
- search. This method is offered on strings. It accepts a daily expression as a parameter, searches if the regular expression pattern exists within the string, and retrieves the index of the primary occurrence of the pattern within the string if it exists.
- exec. This method is offered on regular expressions. It accepts a string as a parameter, searches for the regular expression pattern within the string, and retrieves the outcomes, if there are any.
Conclusion
Regular expressions are very useful for testing if a string comprises a certain pattern or substring. With JavaScript regex matching, you may check if a string is a URL, a date, an IP address, or other types and formats.
In comparison with using other methods like indexOf(), the test() method that’s available on regular expressions gives you more flexibility when testing whether a string matches a pattern or not.
Related reading: