How to use the String.prototype.match() Method In Javascript
In this approach, we are using the match() method along with the regular expression. Here we are checking if str1 is the same as str2 using the regular expression pattern. If they are the same, then the true message is printed; otherwise, the false method is printed.
Syntax:
str1.match(new RegExp(`^${str2}$`)) !== null
Example: This example shows the use of the above-explained approach.
const sameString = (
inputString1,
inputString2) => {
return (
inputString1.match(
new RegExp(
`^${inputString2}$`
)
) !== null
);};
console.log(
sameString("geeks", "geeks")
);
Output
true
JavaScript Program to Check if Two Strings are Same or Not
In this article, we are going to implement a JavaScript program to check whether two strings are the same or not. If they are the same then we will return true else we will return false.
Examples:
Input: str1 = Geeks, str2 = Geeks
Output: True. Strings are the Same
Input: str1 = Geeks, str2 = Geek
Output: False. Strings are not Same
Table of Content
- Using localCompare() Method
- Using startsWith() and endsWith() Methods
- Using the String.prototype.match() Method
- Using Array.from() and Array.prototype.join() Methods
- Using Iterative Character Comparison
Contact Us