Quick Tip: Testing if a String Matches a Regex in JavaScript

In this short tutorial on JavaScript regular expression matching, you’ll learn how to test whether a string matches a regular expression. test() way.

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.

Javascript regex matching allows you to test whether a string contains a specific pattern, substring, or type of characters. Regular expressions are useful for locating information in a string that can be written in a variety of formats, such as dates.

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.

For example:

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 test And regular in that order, and that these words can be separated by zero or more occurrences of any character.

If test() returns true, Matched Logged in to the console. Otherwise, Not Matched Logged in to the console.

since str words include test And regularAnd test beat 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.

see pen
test a string against a regular expression
by sitepoint (@SitePoint,
Feather codepen,

common example

This section shows some examples of how to use JavaScript regex matching to test common use cases. It should be noted that the regular expression here may not be the right solution in every case. Each of them is used to give a simple example of how the process works.

test url

You can test whether a string is a URL using a regular expression. You can experiment with it using the following Codepen demo.

see pen
Check if a string is a URL in JavaScript
by sitepoint (@SitePoint,
Feather codepen,

Please note that the regular expression pattern used above expects the URL to begin with http:// either https://,

test email

You can use a regular expression to check whether a string is a valid email address. The following Codepen demo shows how.

see pen
test is a string is an email in js
by sitepoint (@SitePoint,
Feather codepen,

test dates

You can test whether a string is a date using a regular expression. The following Codepen demo shows how this can be done.

see pen
Check if a string is a date in JavaScript
by sitepoint (@SitePoint,
Feather codepen,

Please note that the regular expression pattern used above expects the date to be in the formats “DD-MM-YYYY” or “DD/MM/YYYY”.

Other Methods for Javascript Regex Matching

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.

conclusion

Regular expressions are very useful for testing if a string contains a certain pattern or substring. With JavaScript regex matching, you can check whether a string is a URL, date, IP address, or other type and format.

compared to using other methods like indexOf()The test() The method that is available on regular expressions gives you more flexibility when testing whether a string matches a pattern.

Related Reading:

Leave a Reply