Matching accented characters with Javascript regexes

nickf picture nickf · Mar 25, 2011 · Viewed 49k times · Source

Here's a fun snippet I ran into today:

/\ba/.test("a") --> true
/\bà/.test("à") --> false

However,

/à/.test("à") --> true

Firstly, wtf?

Secondly, if I want to match an accented character at the start of a word, how can I do that? (I'd really like to avoid using over-the-top selectors like /(?:^|\s|'|\(\) ....)

Answer

Wak picture Wak · Jul 18, 2012

This worked for me:

/^[a-z\u00E0-\u00FC]+$/i

With help from here