Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript regular expression problem with \b and international characters

I'm having a lot of problems with a simple regular expression match.

I have this string with accented characters (this is just an example) "Botó Entrepà Nadó Facebook! " and I want to match words using words from another list.

This is a simplified version of my code. For example to match "Botó"

var matchExpr = new RegExp ('\\b' + 'Botó' + '\\b','i'); 
"Botó Entrepà Nadó Facebook! ".match(matchExpr);

If I run it, it doesn't match "Botó" as expected (Firefox, IE and Chrome).

I thought it was an error on my side. But here comes the fun...

If I modify the string like this "Botón Entrepà Nadó Facebook! " (notice the "n" after "Botó") and I run the same code:

var matchExpr = new RegExp ('\\b' + 'Botó' + '\\b','i'); 
"Botón Entrepà Nadó Facebook! ".match(matchExpr);

It matches "Botó"!!!!????? (at least in Firefox). This does't make sense for me as "n" is NOT a word boundary (that is matched by \b).

If you try to match the whole word:

var matchExpr = new RegExp ('\\b' + 'Botón' + '\\b','i'); 
"Botón Entrepà Nadó Facebook! ".match(matchExpr);

It works.

To make it a little bit more weird, we add another accented letter at the end.

var matchExpr = new RegExp ('\\b' + 'Botóñ' + '\\b','i'); 
"Botóñ Entrepà Nadó Facebook! ".match(matchExpr);

If we try to match this, it matches nothing. BUT, if we try this

var matchExpr = new RegExp ('\\b' + 'Botóñ' + '\\b','i'); 
"Botóña Entrepà Nadó Facebook! ".match(matchExpr);

it matches "Botóñ". Which is wrong.

If we try to match "Facebook" it works as expected. If you try to match words with accents in the middle, it works as expected. But if you try to match words with an accent at the end, it fails.

What am I doing wrong? Is this the expected behaviour?

like image 715
JLP Avatar asked Mar 15 '11 12:03

JLP


1 Answers

Unfortunately, the shorthand character classes in Javascript don't support unicode (or even high ASCII).

Take a look at the answers to this question: Javascript + Unicode. This article, linked in that question, JavaScript, Regex, and Unicode, says that \b is defined by a word boundary, which is defined as:

→ Word character — The characters A-Z, a-z, 0-9, and _ only.
→ Word boundary — The position between a word character and non-word character.

So it will work for words with A-Z, a-z, 0-9, and _ at the end, but not with accented characters at the end.

like image 69
David Fullerton Avatar answered Oct 27 '22 16:10

David Fullerton