jQuery String在TextArea中替换 [英] jQuery String Replace in TextArea

查看:120
本文介绍了jQuery String在TextArea中替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户输入内容后,我尝试在textarea中进行字符串替换,但我尝试过的任何方法都没有用.任何帮助将不胜感激.这是我的位置:

I'm trying to do a string replace in a textarea after the user has entered their content and nothing I've tried is working. Any help would be greatly appreciated. This is where I am:

<textarea id="field_id_29"></textarea>

$("#field_id_29").bind("change keyup input",function(){
     var text = $("#field_id_29").val();
     text = text.replace(/source/g,"www")
     $("#field_id_29").val(text);
});

我需要将 www-源替换为 www .

jsFiddle在这里: http://jsfiddle.net/6RNY2/

jsFiddle is here: http://jsfiddle.net/6RNY2/

推荐答案

尝试一下,

$("#field_id_29").change(function(){
    var text = $(this).val();
    var regexp = /www-source/gi;
    if ( text.match(regexp) ){      
        text = text.replace(/www-source/g,"www");
        return $(this).val(text);
    }
    return false;
});

当字段更改时,我们将获取值,将其与正则表达式进行比较,如果有匹配项,则替换文本,然后完成操作,否则返回.

When the field changes we grab the value, compare if against a regular expression and if we have a match we replace the text and we are done, otherwise we just return.

这篇关于jQuery String在TextArea中替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆