You could dynamically generate a regex from the split string.
This is a basic example which assumes you know the character length of each separator. If the separator is longer or shorter than two characters, it won't work:
const separators = ['XX', 'YY'];
const msg = "Dear XX,Thanks for visting XX"
const userMsg = "Hey Dear ghgh,Thanks for visting jkj"
const dMsg = msg.split(new RegExp(separators.join('|'), 'g'));
const regex = new RegExp(`^${dMsg.join('..')}$`)
console.log(regex)
if (!userMsg.match(regex)) {
console.log("fail");
} else {
console.log("success");
}
^
signifies the start of the string, .
matches any character, so ..
means any two characters, and $
signifies the end of the line.
To compare with separators of any length, you can loop through each separator and replace it with the relevant number of .
characters:
const separators = ['XX', 'YY'];
const msg = "Dear XX,Thanks for visting XX"
const userMsg = "Hey Dear ghgh,Thanks for visting jkj"
let regexStr = msg;
separators.forEach(str => {
const strLength = str.replace(/./g, '.')
regexStr = regexStr.replace(new RegExp(str, 'g'), strLength)
})
const regex = new RegExp(`^${regexStr}$`)
console.log(regex)
if (!userMsg.match(regex)) {
console.log("fail");
} else {
console.log("success");
}
It's important to note, however, that if the length of the string which is in the same position as the separator is of a different length, this won't work.
If this section of the string can be any length (ghgh
in your example), you'd have to use .+
, which matches any character, any amount of times:
const separators = ['XX', 'YY'];
const msg = "Dear XX,Thanks for visting XX"
const userMsg = "Hey Dear ghgh,Thanks for visting jkj"
let regexStr = msg;
separators.forEach(str => {
regexStr = regexStr.replace(new RegExp(str, 'g'), '.+')
})
const regex = new RegExp(`^${regexStr}$`)
console.log(regex)
if (!userMsg.match(regex)) {
console.log("fail");
} else {
console.log("success");
}
Keep in mind, this will match a string like this:
"Dear gh this is also matched! gh,Thanks for visting jkj"
Some regex characters need to be escaped using a backslash \
. For that, you can make use of the replace
method (taken from this SO post):
const separators = ['XX', 'YY'];
const msg = "is the One Time Password (OTP) to authenticate you on IVR for SBI Credit Card ending ty. OTP is valid for 30 mins. Pls do not share this with anyone."
const userMsg = "is the One Time Password (OTP) to authenticate you on IVR for SBI Credit Card ending ty. OTP is valid for 30 mins. Pls do not share this with anyone."
let regexStr = msg;
separators.forEach(str => {
regexStr = regexStr.replace(new RegExp(str, 'g'), '.+')
})
regexStr = regexStr.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
console.log(regexStr)
const regex = new RegExp(`^${regexStr}$`)
console.log(regex)
if (!userMsg.match(regex)) {
console.log("fail");
} else {
console.log("success");
}