const regex = /\b(?<!X-MAS\s)TREE\b/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\b(?<!X-MAS\\s)TREE\\b', 'g')
const str = `X-MAS TREE //this sentence should be excluded
BLA BLA TREE //this sentence should be recognized
XMAS TREE
X-MASTREE
X-TREE
X-MASTREE
How to exclude "X-MAS TREE"?
Because all of those regex, will be replace with "X-MAS TREE"
If I search with keyword "TREE", it will be infinite loop, because "X-MAS TREE" has "TREE"
I tried with this combination, but not working:
\\b(XMAS TREE|X\\-MASTREE|X\\-TREE|TREE|(?!X\\-MAS TREE)\\b
\\b(XMAS TREE|X\\-MASTREE|X\\-TREE|(?!X\\-MAS \\s)TREE)\\b
\\b(XMAS TREE|X\\-MASTREE|X\\-TREE|((?!X\\-MAS )|\\w*)TREE)\\b
\\b(XMAS TREE|X\\-MASTREE|X\\-TREE|(?:(?!X\\-MAS) )TREE)\\b`;
const subst = `X-MAS TREE`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions