-3

I want to make a word boundary \b check for matching words that also work if there are certain special characters in the word getting matched, such as Ø, but preferably not characters like ! or ?. Since the special character is at the end of the word, the \b sees it as being "open space" and therefore matches the word without it actually being a free-standing word.
Is this possible to do?
Right now this check matches both words, but i would like it to only match the word with "Ford".

const nodes = [{
  textContent: "Ford is the best"
}, {
  textContent: "Fordørgen is the best"
}];

const variable = 'Ford';
const regex = new RegExp('\\b' + variable + '\\b');

const matches = nodes.filter(function(node) {
    return regex.test(node.textContent);
});

console.log(matches);
Reaver
  • 323
  • 3
  • 21
  • 1
    Try: `const regex = new RegExp('\\b' + variable + '(?!\\S)');` – anubhava May 04 '21 at 13:41
  • @anubhava this also seems to work, so now im left to wonder if there's a performance difference between these two solutions, or if it really matters which one i choose – Reaver May 04 '21 at 16:15

1 Answers1

0

\b only works for ascii, you have to use unicode properties to handle non-ascii word boundaries, for example:

const nodes = [{
  textContent: "Ford is the best"
}, {
  textContent: "Fordørgen is the best"
}];

const variable = 'Ford';
const regex = new RegExp('(?<!\\p{Alpha})' + variable + '(?!\\p{Alpha})', 'u');

const matches = nodes.filter(function(node) {
    return regex.test(node.textContent);
});

console.log(matches);

(?<!\\p{Alpha}) xxx (?!\\p{Alpha}) means xxx, unless preceded or followed by a unicode alphabetic character.

Docs: Unicode Property Escapes

georg
  • 211,518
  • 52
  • 313
  • 390
  • This looks like exactly what i needed, thanks a lot. I'll accept the answer once i am able to. Do you know if there's any difference in performance between the '\\b' + variable + '(?!\\S)' and this kind of Regex? – Reaver May 04 '21 at 15:44
  • @Reaver: I don't know, but I doubt this part is going to be a bottleneck in your program. – georg May 04 '21 at 17:50