使用javascript执行查找/匹配,而忽略特殊语言字符(例如重音符号)吗? [英] Perform a find/match with javascript, ignoring special language characters (accents, for example)?
本文介绍了使用javascript执行查找/匹配,而忽略特殊语言字符(例如重音符号)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
具有javascript中的stripos函数,例如:
With a stripos function in javascript, such as:
function stripos (f_haystack, f_needle, f_offset) {
var haystack = (f_haystack + '').toLowerCase();
var needle = (f_needle + '').toLowerCase();
var index = 0;
if ((index = haystack.indexOf(needle, f_offset)) !== -1) {
return index;
}
return false;
}
如何使用/重新编码此功能,使其与特殊字符匹配?
How would I use/recode this function so that it matches special characters?
好像:
var haystack = 'Le créme';
var needle = 'reme';
// ^ these two should match (anything other than false)
推荐答案
您可以在搜索之前清理字符串
you can clean the strings before the search
String.prototype.removeAccents = function(){
return this
.replace(/[áàãâä]/gi,"a")
.replace(/[éè¨ê]/gi,"e")
.replace(/[íìïî]/gi,"i")
.replace(/[óòöôõ]/gi,"o")
.replace(/[úùüû]/gi, "u")
.replace(/[ç]/gi, "c")
.replace(/[ñ]/gi, "n")
.replace(/[^a-zA-Z0-9]/g," ");
}
使用:
function stripos (f_haystack, f_needle, f_offset) {
var haystack = (f_haystack + '').toLowerCase().removeAccents();
var needle = (f_needle + '').toLowerCase().removeAccents();
var index = 0;
if ((index = haystack.indexOf(needle, f_offset)) !== -1) {
return index;
}
return false;
}
这篇关于使用javascript执行查找/匹配,而忽略特殊语言字符(例如重音符号)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文