Skip to content Skip to sidebar Skip to footer

How To Ignore Spaces Only For Dynamic Content While Forming An Regular Expression To Compare Two Sentences?

I have a sentence, and I need to compare it with customer send message and return whether it has been passed or failed. Sentence contains {#val#}, which can be replaced by any valu

Solution 1:

var separators = ["#val#", "#VAL#", "#vaL#", "#vAl#", "#Val#"];
let sentence = "Hi {#VAL#}, Thank you {#val#} {#val#} for visting us";
// make all spaces optional. Except "} {".
regexString = sentence.replace(/(^|.)\s($|.)/g, (x, g1, g2) => (x == "} {" ? x : g1 + "\\s?" + g2));
// turn separators into .{0,5}
separators.forEach((str) => {
    regexString = regexString.replace(newRegExp(`{${str}}`, "g"), ".{0,5}");
});
// inputvar customermsg = "Hi asz, Thank you nehra dutta for visting us"; //Should Passvar customermsg1 = "Hiasz, Thank you nehra dutta forvisting us"; //Should Passvar customermsg2 = "Hi asz, Thank you nehradutta for visting us"; //Should Faillet regex = RegExp("^" + regexString + "$");

console.log("REGEX ==>", regex);
console.log(regex.test(customermsg) ? "Pass" : "Fail", "==>", customermsg);
console.log(regex.test(customermsg1) ? "Pass" : "Fail", "==>", customermsg1);
console.log(regex.test(customermsg2) ? "Pass" : "Fail", "==>", customermsg2);

Solution 2:

I had to use two regexes because of the complexity of the conditions but here is my answer.

I explained the regex in comments

const separator = '{#val#}'; // This is what is used to declare dynamic contentconst staticSpace = newRegExp(`(?=[^.\\s*${separator}\\s*.])(\\s|.{0})`, 'i');
// This regex called staticSpace, is for those the spacing between static content, that you said should be ignored.const dynamicSpace = newRegExp(`\\s*(${separator})\\s*`, 'gi');
// This regex called dynamicSpace, is for the possible spacing between static content and dynamic content, that you said shouldn't be too long, so I figured only one space after a dynamic word is alright.// The \s* is used to mark any amount of space// The (${seperator}) will input the seperator variable, this part is crucial cus that's how dynamic content would be marked.// Then the last \s* with the same use to the firstconst sentence = "Hi {#val#}, Thank you {#val#} {#val#} for visting us"; // Template for the messageslet customerMessages = ["Hi asz, Thank you shaky dutta for visting us", "Hiasz, Thank you shaky dutta forvisting   us", "Hi asz, Thank you nehra      dutta for visting us"]; // All the customerMessageslet regex = newRegExp(`^${[].concat.apply([], sentence.split(staticSpace).map(a => a.includes(separator) ? a : a.split(/.{0}|\s/))).filter(e => /\S/.test(e)).join('\\s*').replace(dynamicSpace, '\\s?$1\\s?').replace(newRegExp(separator, 'gi'), '[.\\S]{0,5}')}$`, 'i');
// Firstly, I split the template to the static content to ensure that static spacing is ignored// Then I replaced all the dynamicSpace with \s?1\s? which is just for one space before and after the seperator// Then all the extra spacing, this would be static content spacing which you said can be as long as it wants and I replaced it with \s* which matches 0 or as many as possible because of static content that may be joined together// Finally, I used the seperator to match the dynamic content placeholders. [.\\S]{0,5}, this regex matches any character except a space, with a limit of 5 characters. Because you said that the dynamic content should not be longer than 5 characters// I passed the flag i to most of the replacements because of case-insensitivity. You woulnd't have to define the same seperator in different cases nowconsole.log(customerMessages.map(message => ({
  message,
  state: regex.test(message) ? 'Pass' : 'Fail'
})));
// logging out the result

Post a Comment for "How To Ignore Spaces Only For Dynamic Content While Forming An Regular Expression To Compare Two Sentences?"