function evaluateExpression(expression, text) {
// Step 1: Replace AND, OR, NOT with JS logical operators
expression = expression.replace(/bANDb/g, '&&')
.replace(/bORb/g, '||')
.replace(/bNOTb/g, '!');
// Step 2: Check for existence of words and replace with true or false
const words = expression.match(/b[a-zA-Z]+b/g);
const wordSet = new Set(words);
wordSet.forEach(word => {
const wordExists = new RegExp(`\b${word}\b`, 'i').test(text);
expression = expression.replace(new RegExp(`\b${word}\b`, 'g'), wordExists ? 'true' : 'false');
});
// Step 3: Evaluate the expression
return expression
}
// Example usage
const phrase = "(software) AND (engineer OR developer)";
const text = "I am a software engineer";
expression = evaluateExpression(phrase, text); // Output: true
eval(expression)
the problem is expression is returning a string '(true) && (true || false)'
when I pass this into eval()
, i get an error
how do i convert this into something that eval()
can process?