Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regexp special characters

I have searched and none of the existing answers works for me. My problem is as follows:

I have this code for RegExp that searches for match and highlights matching letters starting at the frist letter:

var newvals = [], regexp = new RegExp('\\b' + search.escapeRegExp(), insensitive ? 'ig' : '');

This works fine for English/US letters, but I also have special characters from the Norwegian alphabet "æøå". Any idea how I can change this regular expression to also cover the special characters?

EDIT: After applying the tip from Sam Saint Pettersen (thank you!), I got it to display the special characters, but when I do a search, the autocomplete now only match uppercase OR lowercase letters. So if I type "Ø" it suggests all words starting with a "Ø" in uppercase, and not the words starting with "ø" in lowercase. The same happens for lowercase search. The regular letters however, displays normally both uppercase and lowercase. This problem only applies to the special characters. Any ideas?

like image 914
user2282217 Avatar asked Mar 24 '23 08:03

user2282217


1 Answers

var re = new RegExp(/[a-z\Wæøå]+/igm);

I tried it against:

Hva heter du?

Hei. Min navn er Søren!

S-Ø-R-E-N.

Jeg bor i et grønn hus og jeg også lærer japansk.

Seemed to match that. At least in http://gskinner.com/RegExr/

I think if you save your JavaScript in UTF-8, this will work. The Unicode escapes for the Norwegian letters are:

  • Æ \u00C6, æ \u00E6
  • Ø \u00D8, ø \u00F8
  • Å \u00C5, å \u00E5

Hope this helps.

like image 195
Sam Saint-Pettersen Avatar answered Apr 01 '23 17:04

Sam Saint-Pettersen