Strings are pieces of text that can contain different types of data — such as URLs, phone numbers, names, numbers, and more. In many cases, you need to test whether a string contains a piece of text or certain types of characters.
You might be inclined to use a method like this when you’re testing whether a string contains a specific substring
indexOf(), However, for more flexible tests and conditions, a better option is to use regular expressions.
Testing Strings Against Regular Expressions
To test whether a string matches a regular expression, you must first create a regular expression instance. Then, you can use
test() method is available on regular expression to check whether the string matches the regular expression or not.
test() The method accepts one parameter: the string to test the pattern against. It returns a boolean value indicating whether the string matches the regular expression.
const pattern = /test.*regular/; const str = 'I want to test this string against a regular expression'; if (pattern.test(str)) console.log('Matched'); else console.log('Not Matched');
In this example, you create the pattern
test.*regular, This pattern means that a string must contain the words
regular in that order, and that these words can be separated by zero or more occurrences of any character.
test() returns true,
Matched Logged in to the console. Otherwise,
Not Matched Logged in to the console.
str words include
regular in a string, it will match against the pattern and
test() Truth will come back.
you can also use regular expression To declare the constructor pattern:
const pattern = new RegExp('test.*regular'); const str = 'I want to test this string against a regular expression'; if (pattern.test(str)) console.log('Matched'); else console.log('Not Matched');
You can test it out in the following Codepen demo.
You can test whether a string is a URL using a regular expression. You can experiment with it using the following Codepen demo.
Please note that the regular expression pattern used above expects the URL to begin with
You can use a regular expression to check whether a string is a valid email address. The following Codepen demo shows how.
You can test whether a string is a date using a regular expression. The following Codepen demo shows how this can be done.
Please note that the regular expression pattern used above expects the date to be in the formats “DD-MM-YYYY” or “DD/MM/YYYY”.
There are other ways to test whether a string matches a regular expression. This article doesn’t cover them all, but here’s a brief description of them:
- match, This method is available on Strings. It accepts a regular expression as a parameter and retrieves the parts of the string that match the regular expression, if any.
- Search, This method is available on Strings. It accepts a regular expression as a parameter, searches whether the regular expression pattern is present in the string, and retrieves the index of the first occurrence of the pattern in the string if it is present.
- executive, This method is available on regular expressions. It accepts a string as a parameter, searches for a regular expression pattern in the string, and returns the result if any.
compared to using other methods like
test() The method that is available on regular expressions gives you more flexibility when testing whether a string matches a pattern.