How to compare a sentence by creating regular expressions except few chars in NodeJS?

I have a phrase:

let msg = "Dear XX,Thanks for visting XX"
let usermsg = "Dear sd dta,Thanks for visting ght hg"

I need to compare these two sentences, ignoring “XX” and “YY”. How can I create a regular expression to compare both of these in Nodejs?

I have replaced “XX” with an empty space and tried whether the usermsg includes msg or not. But that doesn’t work.

var separators = ['XX','YY'];
let dMsg = msg.split(new RegExp(separators.join('|'),'g'));
for (var p = 0; p < dMsg.length; p++) {       
    if (!(usermsg.includes(dMsg[p]))) {
        console.log("fail");
        break;
    }
}

It doesn’t work if the user adds an extra string in front, like:

usermsg2 = "Hey Dear ghgh,Thanks for visting jkj",

In the above usermsg2, “Hey” should not be there, but my code returns that both are the same.

Kindly suggest how to check these?

Answer

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");
}