最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - regexp special characters - Stack Overflow

programmeradmin3浏览0评论

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 autoplete 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?

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 autoplete 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?

Share Improve this question edited Apr 16, 2013 at 7:11 user2282217 asked Apr 15, 2013 at 10:58 user2282217user2282217 912 silver badges10 bronze badges 2
  • That's Javascript not Java! Please edit the tags in your question. – UltraInstinct Commented Apr 15, 2013 at 11:03
  • 3 Check out this: stackoverflow./questions/280712/javascript-unicode – BlitZ Commented Apr 15, 2013 at 11:04
Add a ment  | 

1 Answer 1

Reset to default 7
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./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.

发布评论

评论列表(0)

  1. 暂无评论