Check if a string conforms to a regex pattern in JavaScript
This post will discuss how to check if a string conforms to a regex pattern in JavaScript.
There are several methods to check if a string conforms to a regex pattern in JavaScript. A regex, or regular expression, is a pattern that can be used to match or search for characters or sequences of characters in a string. Here are some of the methods that we can use:
1. Using test() function
We can use the test() function of the RegExp object, which takes a string as an argument and returns a boolean value indicating whether the string matches the regex or not. It returns true if the string matches the regex, and false otherwise. For example, to check whether the string "Hello, world" matches the regex /world/:
|
1 2 3 4 5 6 7 |
let str = "Hello world"; // A string variable let regex = /world/; // A regex variable // Check if the string matches the regex using test() let result = regex.test(str); console.log(result); // true |
2. Using match() function
We can use the match() function of the String object, which takes a regex as an argument and returns an array containing the matches or null if there are no matches. The array will contain the whole matched string as the first element, and any captured groups as the subsequent elements. We can use this function to get more information about the match, such as the index and the input string. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 |
// A string variable let str = "Hello world"; // A regex variable with two capture groups let regex = /(\w+) (\w+)/; // Match the string with the regex using match let result = str.match(regex); // Prints ["Hello world", "Hello", "world", index: 0, input: "Hello world", groups: undefined] console.log(result); |
3. Using exec() function
We can use the exec() function of the RegExp object, which also takes a string as an argument and returns a similar array as the match() function or null if there are no matches. The difference is that this function also updates the properties of the RegExp object, such as lastIndex and global. This function is useful when we want to perform multiple matches on the same string using a regex with the global flag (g). Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// A string variable let str = "Hello world"; // A regex variable with two capture groups and a global flag let regex = /(\w+) (\w+)/g; // A variable to store the result let result; // Loop over the matches using exec while ((result = regex.exec(str)) !== null) { // ["Hello world", "Hello", "world", index: 0, input: "Hello world", groups: undefined] console.log(result); // Prints 11 console.log(regex.lastIndex); } |
That’s all about checking if a string conforms to a regex pattern in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)