简体   繁体   English

正则表达式特殊字符

[英]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: 我有RegExp的这段代码,用于搜索匹配项并突出显示从第一个字母开始的匹配字母:

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. 编辑:应用了Sam Saint Pettersen的技巧(谢谢!)后,我得到它来显示特殊字符,但是当我执行搜索时,自动完成功能现在仅匹配大写或小写字母。 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? 有任何想法吗?

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

I tried it against: 我尝试过:

Hva heter du? Hva heter du?

Hei. Min navn er Søren! Min navn erSøren!

S-Ø-REN. S-Ø-REN。

Jeg bor i et grønn hus og jeg også lærer japansk. Jeg bor i etgrønnhus og jegogsålærerjapansk。

Seemed to match that. 似乎匹配。 At least in http://gskinner.com/RegExr/ 至少在http://gskinner.com/RegExr/中

I think if you save your JavaScript in UTF-8, this will work. 我认为,如果将JavaScript保存在UTF-8中,则可以使用。 The Unicode escapes for the Norwegian letters are: 挪威字母的Unicode转义为:

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM