jQuery的查找和使用键值阵列取代 [英] jQuery find and replace with key value arrays

查看:100
本文介绍了jQuery的查找和使用键值阵列取代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发现这个code的查找和替换

found this code for find and replace

var f = ['Rd','St','Ave'];
var r = ['Road','Street','Avenue'];

var re = $.map(f, function(v,i) {
return new RegExp('\\b' + v + '\\b', 'g');
});

jQuery('#colCenterAddress').val(function(i,val) {
$.each(f,function(i,v) {
    val = val.replace(re[i],r[i]);
});
return val;
});

的jQuery查找并替换阵列
谢谢
<一href=\"http://stackoverflow.com/users/1106925/squint\">http://stackoverflow.com/users/1106925/squint

其真正伟大的,但我想重构使用键值阵列

its really great but i would like to refactor to use a key value array

var fr ={ 
 "Rd" : "road",
 "St" : "Street",
 "Ave" : "Avenue",
 };

firly新的jQuery任何帮助将是巨大的。

firly new to jQuery any help would be great

推荐答案

尝试

var re = $.map(fr, function (v, k) {
    return {
        regex: new RegExp('\\b' + k + '\\b', 'g'),
        value: v
    };
});
jQuery('#colCenterAddress').val(function (i, val) {
    $.each(re, function (i, obj) {
        val = val.replace(obj.regex, obj.value);
    });
    return val;
});

演示:小提琴

这篇关于jQuery的查找和使用键值阵列取代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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