替换 javascript 中的 unicode 匹配项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14613080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace unicode matches in javascript
提问by Adam
I would like to replace the matched words/characters found in a search, for example if I search for a
and I get the result ádám
, I would like to highlight the á
's. Something like:
我想替换在搜索中找到的匹配单词/字符,例如,如果我搜索a
并得到结果ádám
,我想突出显示á
's。就像是:
"ádám".replace(/(a)/gi, '*') // -> should return "*d*m"
But of course this doesn't works. I'm trying out XRegExphowever I just can't figure it out.
但这当然行不通。我正在尝试XRegExp但我就是想不通。
How would you do it for all languages? Is it possible?
你会如何为所有语言做到这一点?是否可以?
回答by MikeM
Adapted from Semplice, following link from here.
[^\x00-\x80]
matches any character not in the ASCII range.
Note that some of the characters may not be encoded correctly from the copy and paste.
[^\x00-\x80]
匹配任何不在 ASCII 范围内的字符。
请注意,某些字符可能无法通过复制和粘贴正确编码。
var latin_map = {"á":"A","?":"A","?":"A","?":"A","?":"A","?":"A","?":"A","ǎ":"A","?":"A","?":"A","?":"A","?":"A","?":"A","?":"A","?":"A","?":"A","?":"A","?":"A","?":"A","?":"A","à":"A","?":"A","?":"A","ā":"A","?":"A","?":"A","?":"A","?":"A","?":"A","?":"A","?":"AA","?":"AE","?":"AE","?":"AE","?":"AO","?":"AU","?":"AV","?":"AV","?":"AY","?":"B","?":"B","?":"B","?":"B","?":"B","?":"B","?":"C","?":"C","?":"C","?":"C","?":"C","?":"C","?":"C","?":"C","?":"D","?":"D","?":"D","?":"D","?":"D","?":"D","?":"D","?":"D","?":"D","?":"D","?":"D","?":"DZ","?":"DZ","é":"E","?":"E","ě":"E","?":"E","?":"E","ê":"E","?":"E","?":"E","?":"E","?":"E","?":"E","?":"E","?":"E","?":"E","?":"E","?":"E","è":"E","?":"E","?":"E","ē":"E","?":"E","?":"E","?":"E","?":"E","?":"E","?":"E","?":"ET","?":"F","?":"F","?":"G","?":"G","?":"G","?":"G","?":"G","?":"G","?":"G","?":"G","?":"G","?":"H","?":"H","?":"H","?":"H","?":"H","?":"H","?":"H","?":"H","?":"H","í":"I","?":"I","ǐ":"I","?":"I","?":"I","?":"I","?":"I","?":"I","?":"I","ì":"I","?":"I","?":"I","ī":"I","?":"I","?":"I","?":"I","?":"I","?":"D","?":"F","?":"G","?":"R","?":"S","?":"T","?":"IS","?":"J","?":"J","?":"K","?":"K","?":"K","?":"K","?":"K","?":"K","?":"K","?":"K","?":"K","?":"K","?":"L","?":"L","?":"L","?":"L","?":"L","?":"L","?":"L","?":"L","?":"L","?":"L","?":"L","?":"L","?":"L","?":"L","?":"LJ","?":"M","?":"M","?":"M","?":"M","?":"N","?":"N","?":"N","?":"N","?":"N","?":"N","?":"N","?":"N","?":"N","?":"N","?":"N","?":"N","?":"NJ","ó":"O","?":"O","ǒ":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","ò":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","ō":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"O","?":"OI","?":"OO","?":"E","?":"O","?":"OU","?":"P","?":"P","?":"P","?":"P","?":"P","?":"P","?":"P","?":"Q","?":"Q","?":"R","?":"R","?":"R","?":"R","?":"R","?":"R","?":"R","?":"R","?":"R","?":"R","?":"R","?":"C","?":"E","?":"S","?":"S","?":"S","?":"S","?":"S","?":"S","?":"S","?":"S","?":"S","?":"S","?":"T","?":"T","?":"T","?":"T","?":"T","?":"T","?":"T","?":"T","?":"T","?":"T","?":"T","?":"A","?":"L","?":"M","?":"V","?":"TZ","ú":"U","?":"U","ǔ":"U","?":"U","?":"U","ü":"U","ǘ":"U","ǚ":"U","ǜ":"U","ǖ":"U","?":"U","?":"U","?":"U","?":"U","ù":"U","?":"U","?":"U","?":"U","?":"U","?":"U","?":"U","?":"U","?":"U","ū":"U","?":"U","?":"U","?":"U","?":"U","?":"U","?":"U","?":"V","?":"V","?":"V","?":"V","?":"VY","?":"W","?":"W","?":"W","?":"W","?":"W","?":"W","?":"W","?":"X","?":"X","Y":"Y","?":"Y","?":"Y","?":"Y","?":"Y","?":"Y","?":"Y","?":"Y","?":"Y","?":"Y","?":"Y","?":"Y","?":"Z","?":"Z","?":"Z","?":"Z","?":"Z","?":"Z","?":"Z","?":"Z","?":"Z","?":"IJ","?":"OE","?":"A","?":"AE","?":"B","?":"B","?":"C","?":"D","?":"E","?":"F","?":"G","?":"G","?":"H","?":"I","?":"R","?":"J","?":"K","?":"L","?":"L","?":"M","?":"N","?":"O","?":"OE","?":"O","?":"OU","?":"P","?":"R","?":"N","?":"R","?":"S","?":"T","?":"E","?":"R","?":"U","?":"V","?":"W","?":"Y","?":"Z","á":"a","?":"a","?":"a","?":"a","?":"a","?":"a","?":"a","ǎ":"a","a":"a","?":"a","?":"a","?":"a","?":"a","?":"a","?":"a","?":"a","?":"a","?":"a","?":"a","?":"a","à":"a","?":"a","?":"a","ā":"a","?":"a","?":"a","?":"a","?":"a","?":"a","?":"a","?":"a","?":"a","?":"aa","?":"ae","?":"ae","?":"ae","?":"ao","?":"au","?":"av","?":"av","?":"ay","?":"b","?":"b","?":"b","?":"b","?":"b","?":"b","?":"b","?":"b","?":"o","?":"c","?":"c","?":"c","?":"c","?":"c","?":"c","?":"c","?":"c","?":"c","?":"d","?":"d","?":"d","?":"d","?":"d","?":"d","?":"d","?":"d","?":"d","?":"d","?":"d","?":"d","?":"d","?":"d","?":"i","?":"j","?":"j","?":"j","?":"dz","?":"dz","é":"e","?":"e","ě":"e","?":"e","?":"e","ê":"e","?":"e","?":"e","?":"e","?":"e","?":"e","?":"e","?":"e","?":"e","?":"e","?":"e","è":"e","?":"e","?":"e","ē":"e","?":"e","?":"e","?":"e","?":"e","?":"e","?":"e","?":"e","?":"e","?":"et","?":"f","?":"f","?":"f","?":"f","?":"g","?":"g","?":"g","?":"g","?":"g","?":"g","?":"g","?":"g","?":"g","?":"g","?":"h","?":"h","?":"h","?":"h","?":"h","?":"h","?":"h","?":"h","?":"h","?":"h","?":"h","?":"hv","í":"i","?":"i","ǐ":"i","?":"i","?":"i","?":"i","?":"i","?":"i","ì":"i","?":"i","?":"i","ī":"i","?":"i","?":"i","?":"i","?":"i","?":"i","?":"d","?":"f","?":"g","?":"r","?":"s","?":"t","?":"is","?":"j","?":"j","?":"j","?":"j","?":"k","?":"k","?":"k","?":"k","?":"k","?":"k","?":"k","?":"k","?":"k","?":"k","?":"k","?":"l","?":"l","?":"l","?":"l","?":"l","?":"l","?":"l","?":"l","?":"l","?":"l","?":"l","?":"l","?":"l","?":"l","?":"l","?":"l","?":"l","?":"lj","?":"s","?":"s","?":"s","?":"s","?":"m","?":"m","?":"m","?":"m","?":"m","?":"m","ń":"n","ň":"n","?":"n","?":"n","?":"n","?":"n","?":"n","?":"n","?":"n","?":"n","?":"n","?":"n","?":"n","?":"n","?":"n","?":"nj","ó":"o","?":"o","ǒ":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","ò":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","ō":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"o","?":"oi","?":"oo","?":"e","?":"e","?":"o","?":"o","?":"ou","?":"p","?":"p","?":"p","?":"p","?":"p","?":"p","?":"p","?":"p","?":"p","?":"q","?":"q","?":"q","?":"q","?":"r","?":"r","?":"r","?":"r","?":"r","?":"r","?":"r","?":"r","?":"r","?":"r","?":"r","?":"r","?":"r","?":"r","?":"r","?":"r","?":"c","?":"c","?":"e","?":"r","?":"s","?":"s","?":"s","?":"s","?":"s","?":"s","?":"s","?":"s","?":"s","?":"s","?":"s","?":"s","?":"s","?":"s","ɡ":"g","?":"o","?":"o","?":"u","?":"t","?":"t","?":"t","?":"t","?":"t","?":"t","?":"t","?":"t","?":"t","?":"t","?":"t","?":"t","?":"t","?":"t","?":"t","?":"th","?":"a","?":"ae","?":"e","?":"g","?":"h","?":"h","?":"h","?":"i","?":"k","?":"l","?":"m","?":"m","?":"oe","?":"r","?":"r","?":"r","?":"r","?":"t","?":"v","?":"w","?":"y","?":"tz","ú":"u","?":"u","ǔ":"u","?":"u","?":"u","ü":"u","ǘ":"u","ǚ":"u","ǜ":"u","ǖ":"u","?":"u","?":"u","?":"u","?":"u","ù":"u","?":"u","?":"u","?":"u","?":"u","?":"u","?":"u","?":"u","?":"u","ū":"u","?":"u","?":"u","?":"u","?":"u","?":"u","?":"u","?":"u","?":"ue","?":"um","?":"v","?":"v","?":"v","?":"v","?":"v","?":"v","?":"v","?":"vy","?":"w","?":"w","?":"w","?":"w","?":"w","?":"w","?":"w","?":"w","?":"x","?":"x","?":"x","y":"y","?":"y","?":"y","?":"y","?":"y","?":"y","?":"y","?":"y","?":"y","?":"y","?":"y","?":"y","?":"y","?":"z","?":"z","?":"z","?":"z","?":"z","?":"z","?":"z","?":"z","?":"z","?":"z","?":"z","?":"z","?":"z","?":"z","?":"ff","?":"ffi","?":"ffl","?":"fi","?":"fl","?":"ij","?":"oe","?":"st","?":"a","?":"e","?":"i","?":"j","?":"o","?":"r","?":"u","?":"v","?":"x"};
function embolden( str, chr ){
return str.replace( /[^\x00-\x80]/g,
function (a) {
return chr == latin_map[a] ? '<b>' + a + '</b>' : a;
}
);
}
embolden( 'ádám', 'a' ); // "<b>á</b>d<b>á</b>m"
回答by Danilo Valente
I've tried this code, see if it's what you're looking for:
我试过这段代码,看看它是否是你要找的:
'ádám'.replace(/./g,function(char){
switch(char.toLowerCase()){
case 'á':
case 'à':
case 'a':
case '?':
return '*';
break;
}
return char;
});
EDIT:
编辑:
To replace allchars that don't belong to the ASCII table, just check if the char has a char code up to 127
, since the ASCII table char codes are defined between 0
and 127
(notice that á
doesn't belong to the Unicode table, but to the Extended ASCII table, that comes from 0
up to 255
):
要替换所有不属于 ASCII 表的字符,只需检查该字符的字符代码是否高达127
,因为 ASCII 表字符代码是在0
和之间定义的127
(注意á
不属于 Unicode 表,而是扩展 ASCII 表,来自0
最多255
):
'ádám'.replace(/./g,function(char){
return char.charCodeAt(0)<=127 ? char : '<b>' + char + '</b>';
});
回答by Esailija
A more robust way is to use unicode normalizationfor this:
一个更健壮的方法是为此使用unicode 规范化:
var regex = /[a-z][\u0300-\u036F]+/ig;
UNorm.nfd("ádám").replace( regex, "*" ); //*d*m
Or for actual highlighting:
或用于实际突出显示:
UNorm.nfd("ádám").replace( regex, function(m){
return "*" + UNorm.nfc(m) + "*";
});
//"*á*d*á*m"
For a javascript library: https://github.com/walling/unorm
对于 javascript 库:https: //github.com/walling/unorm
回答by Bhavik Mulia
You can use /([^" "]|\w)/g regex for not replacing white space
您可以使用 /([^" "]|\w)/g 正则表达式不替换空格
回答by Andy Ray
If you're just looking to replace non a-z letters, you can do
如果您只是想替换非 az 字母,则可以
"ádám".replace(/[^a-zA-Z]/g, '*')
"ádám".replace(/[^a-zA-Z]/g, '*')
This question is also a possible dupe of Remove accents/diacritics in a string in JavaScript(voting to close now)
这个问题也可能是在 JavaScript中删除字符串中的重音/变音符号的欺骗(现在投票结束)