Search This Blog

2024/03/21

Javascript Regular Expression:Can two Regx with same pattern are equal ?

Even if you created two regular expression as below with same pattern they
do not match with each other but there "source" property & "toString()"
matches.
Code:
var pattern1 = /^\d{2}-\d{4}-\d{6}$/;
var pattern2 = /^\d{2}-\d{4}-\d{6}$/;
if (pattern1 == pattern2) {
console.log("RAW Pattern Matches");
} else {
console.log("RAW Pattern Does Not Matches");
}
if (pattern1.toString() == pattern2.toString()) {
console.log("Pattern.toString() Matches");
} else {
console.log("Pattern.toString() Does Not Matches");
}
if (pattern1.source == pattern2.source) {
console.log("Pattern.source Matches");
} else {
console.log("Pattern.source Does Not Matches");
}
Output:
RAW Pattern Does Not Matches
Pattern.toString() Matches
Pattern.source Matches

No comments:

Post a Comment