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

javascript - jQuery replace all single character - Stack Overflow

programmeradmin0浏览0评论

I want to replace all characters in the textarea by a click using jQuery.

For example:

ə = e, ı = i, ...

Thıs ıs əxamplə

By clicking it should be:

This is example

I want to replace all characters in the textarea by a click using jQuery.

For example:

ə = e, ı = i, ...

Thıs ıs əxamplə

By clicking it should be:

This is example

Share Improve this question asked Mar 22, 2011 at 17:31 seferovseferov 4,1613 gold badges41 silver badges76 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3
$('textarea').html($('textarea').html().replace(/ə/g,'e'))

Adding on from 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;
});

DEMO


UPDATED EDIT

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

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

UPDATED DEMO

HTML:

<textarea>Thıs ıs əxamplə</textarea>

JS:

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

$('textarea').click(function(){
    this.value = this.value.replace(/./g,function(str){
        return replace_map[str] || str;
    })
});

I don't think you really need jQuery for that other than perhaps to select the textarea element (and then only for a microscopic amount of ease).

Past that you should be able to use just string.replace on the textarea content: https://developer.mozilla/en/JavaScript/Reference/Global_Objects/String/replace

发布评论

评论列表(0)

  1. 暂无评论