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 values in the customer send messages. Condition here is in place of {#val#} in a sentence => Customer message can contain anything within the limit of 5. {#val#} is the dynamic content. Other part of the messages are static content. In static content of the customer message, we can ignore the space and compare with the sentence defined.
But in dynamic content of the customer send messages,({#val#}) spaces should be considered. For example, sentence contains {#val#}{#val#} and in customer msg it should be replaced by nehradutta not nehra space dutta since 2 {#val#}{#val#}s are continuosly put up in the defined sentence.
var sentence = "Hi {#val#}, Thank you {#val#} {#val#} for visting us"
var customermsg = "Hi asz, Thank you shaky dutta for visting us" //Should Pass as sentence and customer msg are matching
var customermsg1 = "Hiasz, Thank you shaky dutta forvisting us" //Should Pass as sentence and customer msg are matching ( Ignoring the space in static portion )
var customermsg2 = "Hi asz, Thank you nehra dutta for visting us" //Should Fail since there is lot of space between the dynamic content {#val#} => (nehra dutta) places. Should contain single space since the sentence has {#val#}space{#val#}.
I need to form a regular expression, which would avoid spaces while comparing static content and include spaces while comparing dynamic content.
Currently my code is below replacing all the spaces and comparing.
var separators = ['#val#','#VAL#','#vaL#','#vAl#','#Val#'];
var regexStr = sentence.replace(/[\ ]/g,'');
var customermsg = customermsg.replace(/[\ ]/g,'');
separators.forEach(str => {
regexStr = regexStr.replace(new RegExp(str, 'g'), '.{0,5}')
})
var regex = new RegExp(`^${regexStr }$`,"i")
if (!customermsg.match(regex))
Status = "Fail"
else
Status = "Pass"
Kindly help me on this as I'm new to regular expression
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(new RegExp(`{${str}}`, "g"), ".{0,5}");
});
// input
var customermsg = "Hi asz, Thank you nehra dutta for visting us"; //Should Pass
var customermsg1 = "Hiasz, Thank you nehra dutta forvisting us"; //Should Pass
var customermsg2 = "Hi asz, Thank you nehradutta for visting us"; //Should Fail
let 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);
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 content
const staticSpace = new RegExp(`(?=[^.\\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 = new RegExp(`\\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 first
const sentence = "Hi {#val#}, Thank you {#val#} {#val#} for visting us"; // Template for the messages
let 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 customerMessages
let regex = new RegExp(`^${[].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(new RegExp(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 now
console.log(customerMessages.map(message => ({
message,
state: regex.test(message) ? 'Pass' : 'Fail'
})));
// logging out the result