Saturday, September 24, 2022
HomeWebsite DesignFast Tip: Testing if a String Matches a Regex in JavaScript

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


On this transient tutorial on JavaScript regex matching, you’ll discover ways to check whether or not a string matches an everyday expression utilizing the check() technique.

Strings are items of textual content that may comprise a wide range of knowledge — equivalent to URLs, cellphone numbers, names, numbers, and extra. In lots of instances, it’s essential to test whether or not or not a string incorporates a chunk of textual content or sure kinds of characters.

Once you’re testing if a string incorporates a selected substring, you could be inclined to make use of a way like indexOf(). Nevertheless, for extra versatile testing and circumstances, utilizing common expressions is a greater possibility.

JavaScript regex matching means that you can test if a string incorporates a selected sample, substring, or kinds of characters. Common expressions are helpful for detecting info in a string that may be written in several codecs, equivalent to dates.

Testing Strings in opposition to Common Expressions

To check whether or not a string matches an everyday expression, you have to first create an everyday expression occasion. Then, you should use the check() technique accessible on the common expression to test if the string matches the common expression or not.

The check() technique accepts one parameter: the string to check in opposition to the sample. It returns a Boolean worth indicating whether or not the string matches the common expression or not.

For instance:

const sample = /check.*common/;
const str = 'I need to check this string in opposition to an everyday expression';
if (sample.check(str)) {
  console.log('Matched');
} else {
  console.log('Not Matched');
}

On this instance, you create the sample check.*common. This sample implies that a string should comprise the phrases check and common in that order, and that these phrases might be separated by zero or extra occurrences of any character.

If check() returns true, Matched is logged within the console. In any other case, Not Matched is logged within the console.

Since str incorporates the phrases check and common, and check precedes common within the string, it should match in opposition to the sample and check() will return true.

It’s also possible to use the RegExp constructor to declare the patterns:

const sample = new RegExp('check.*common');
const str = 'I need to check this string in opposition to an everyday expression';
if (sample.check(str)) {
  console.log('Matched');
} else {
  console.log('Not Matched');
}

You’ll be able to check this out within the following CodePen demo.

See the Pen
Testing a String Towards a Common Expression
by SitePoint (@SitePoint)
on CodePen.

Frequent Examples

This part exhibits some examples of the right way to use JavaScript regex matching to check widespread use instances. It needs to be famous that the common expressions right here may not be the proper resolution in every case. They’re every used to offer a easy instance of how the method works.

Testing URLs

You’ll be able to check if a string is a URL utilizing common expressions. You’ll be able to experiment with this utilizing the next CodePen demo.

See the Pen
Take a look at if a String is a URL in JavaScript
by SitePoint (@SitePoint)
on CodePen.

Please notice that the common expression sample used above expects the URL to start with http:// or https://.

Testing Emails

You’ll be able to check if a string is a legitimate e mail tackle utilizing common expressions. The next CodePen demo exhibits how.

See the Pen
Take a look at is a String is an E mail in JS
by SitePoint (@SitePoint)
on CodePen.

Testing Dates

You’ll be able to check if a string is a date utilizing common expressions. The next CodePen demo exhibits how it may be finished.

See the Pen
Take a look at if a String is a date in JavaScript
by SitePoint (@SitePoint)
on CodePen.

Please notice that the common expression sample used above expects the date to be of the codecs “DD-MM-YYYY” or “DD/MM/YYYY”.

Different Strategies for JavaScript Regex Matching

There are different strategies to check whether or not a string matches an everyday expression. This text doesn’t cowl all of them, however right here’s a quick overview of them:

  • match. This technique is on the market on strings. It accepts an everyday expression as a parameter and retrieves the components of the string that match the common expression, if there are any.
  • search. This technique is on the market on strings. It accepts an everyday expression as a parameter, searches if the common expression sample exists within the string, and retrieves the index of the primary prevalence of the sample within the string if it exists.
  • exec. This technique is on the market on common expressions. It accepts a string as a parameter, searches for the common expression sample within the string, and retrieves the outcomes, if there are any.

Conclusion

Common expressions are very helpful for testing if a string incorporates a sure sample or substring. With JavaScript regex matching, you may test if a string is a URL, a date, an IP tackle, or different sorts and codecs.

In comparison with utilizing different strategies like indexOf(), the check() technique that’s accessible on common expressions offers you extra flexibility when testing whether or not a string matches a sample or not.

Associated studying:

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments