Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery替换所有单个字符_Javascript_Jquery - Fatal编程技术网

Javascript jQuery替换所有单个字符

Javascript jQuery替换所有单个字符,javascript,jquery,Javascript,Jquery,我想使用jQuery通过单击替换文本区域中的所有字符 例如: ə=e,ı=i 萨姆普 通过单击,它应该是: 这就是一个例子,我认为除了选择textarea元素(然后只是为了简单起见)之外,您并不需要jQuery 超过此值时,您应该能够在textarea内容上仅使用string.replace: HTML: <textarea>Thıs ıs əxamplə</textarea> 从Zikes添加 var replace_map={ "ı":"i", "ə

我想使用jQuery通过单击替换文本区域中的所有字符

例如:

ə=e,ı=i

萨姆普

通过单击,它应该是:


这就是一个例子,我认为除了选择textarea元素(然后只是为了简单起见)之外,您并不需要jQuery

超过此值时,您应该能够在textarea内容上仅使用string.replace:

HTML:

<textarea>Thıs ıs əxamplə</textarea>
从Zikes添加

var replace_map={
    "ı":"i",
    "ə":"e"
};

$('textarea').click(function(){
    var ret='';
    $.each(this.value.split(''), function(i, str) {
        ret += replace_map[str] || str;
    })
    this.value = ret;
});


更新编辑

var replace_map={
    "ı":"i",
    "ə":"e"
};

$('textarea').click(function(){
     this.value = $.map(this.value.split(''), function(str) {
        return replace_map[str] || str;
    }).join('');
});

如果只需逐个字符迭代字符串,而不使用正则表达式,我将+1。最近每个人似乎都太快了,无法使用正则表达式。@Brad Christie,它可能没有逐字符迭代那么快,但除非textarea值的大小为几十KB,否则我不认为在这种情况下性能差异会有那么大。这正是我想要的。谢谢
var replace_map={
    "ı":"i",
    "ə":"e"
};

$('textarea').click(function(){
    var ret='';
    $.each(this.value.split(''), function(i, str) {
        ret += replace_map[str] || str;
    })
    this.value = ret;
});
var replace_map={
    "ı":"i",
    "ə":"e"
};

$('textarea').click(function(){
     this.value = $.map(this.value.split(''), function(str) {
        return replace_map[str] || str;
    }).join('');
});