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|'|\(\) ....
)
match() is an inbuilt function in JavaScript used to search a string for a match against any regular expression. If the match is found, then this will return the match as an array. Parameters: Here the parameter is “regExp” (i.e. regular expression) which will compare with the given string.
Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ).
replace(/[^a-z0-9]/gi,'') . However a more intuitive solution (at least for the user) would be to replace accented characters with their "plain" equivalent, e.g. turn á , á into a , and ç into c , etc.
You need to escape the / with a \ . Show activity on this post. You can escape it by preceding it with a \ (making it \/ ), or you could use new RegExp('/') to avoid escaping the regex.
This worked for me:
/^[a-z\u00E0-\u00FC]+$/i
With help from here
The reason why /\bà/.test("à")
doesn't match is because "à" is not a word character. The escape sequence \b
matches only between a boundary of word character and a non word character. /\ba/.test("a")
matches because "a" is a word character. Because of that, there is a boundary between the beginning of the string (which is not a word character) and the letter "a" which is a word character.
Word characters in JavaScript's regex is defined as [a-zA-Z0-9_]
.
To match an accented character at the start of a string, just use the ^
character at the beginning of the regex (e.g. /^à/
). That character means the beginning of the string (unlike \b
which matches at any word boundary within the string). It's most basic and standard regular expression, so it's definitely not over the top.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With