1

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?

Daniel_Knights
  • 7,940
  • 4
  • 21
  • 49
sai
  • 487
  • 1
  • 7
  • 16
  • You're missing a closing quote: `"fail"` and you don't need to use `break` inside an `if` statement. Aside from these issues, your code seems to work. – Daniel_Knights Jan 07 '21 at 11:58
  • @Daniel_Knights I have updated the code, this doesnt work for all th scenarios. Please help. – sai Jan 07 '21 at 12:06

1 Answers1

1

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");
}
Daniel_Knights
  • 7,940
  • 4
  • 21
  • 49
  • Thank you so much for the detailed explanation, but in my case, separator can be of any length like "Dear Sara Duts" – sai Jan 07 '21 at 12:27
  • https://stackoverflow.com/questions/66004421/how-to-ignore-spaces-only-for-dynamic-content-while-forming-an-regular-expressio – sai Feb 02 '21 at 10:44
  • Hi, Could you please take a look at this? https://stackoverflow.com/questions/66004421/how-to-ignore-spaces-only-for-dynamic-content-while-forming-an-regular-expressio – sai Feb 02 '21 at 10:45