

It accepts a string which we have to test against regular expression and returns true or false depending upon if the match is found or not.įor example: var regex = /hello/ var str = 'hello world' var result = regex.test(str) console.log(result) // returns true () This method is used to test whether a match has been found or not. There are mainly two methods for testing regular expressions. Since forward slashes are used to enclose patterns in the above example, you have to escape the forward slash ( / ) with a backslash ( \ ) if you want to use it as a part of the regex. Both regex objects will have same methods and properties attached to them. No matter which method you choose, the result is going to be a regex object. There might also be cases where you want to create regular expressions dynamically, in which case regex literal won’t work, so you have to use a regular expression constructor.

Regular Expression Constructor:Įxample: var regexConst = new RegExp('abc') Regular Expression Literal:

It can be either created with RegExp constructor, or by using forward slashes ( / ) to enclose the pattern. There are two ways to create a regular expression in Javascript. Regular expressions allow you to check a string of characters like an e-mail address or password for patterns, to see so if they match the pattern defined by that regular expression and produce actionable information. They form a small language of its own, which is a part of many programming languages like Javascript, Perl, Python, Php, and Java. Regular expressions are a way to describe patterns in a string data.
