+ -
当前位置:首页 → 问答吧 → 问一个jq的正则提取字符问题

问一个jq的正则提取字符问题

时间:2011-10-21

来源:互联网

JScript code


//数字变色
$(function () {
            $(".info .info_content span[class='money']").each(function () {
                var str = $(this).html().replace(/([^0-9]+)/gi, '');
                var str1 = $(this).html().replace(/([0-9]+)+/gi, '');
                var strdiv = "<span style='color:red;font-size:16px;font-family:Adobe 黑体 Std R'>" + str + "</span>" + "<span style='font-size:12px;'>"+str1+"</span>";
                $(this).html(strdiv);
            });
        });




以上代码,当要处理的内容为 100元/月 时可以将 100和后面的 元/月 分离出来,并重新设置样式,但是如果内容为 1.1 元/月时,就会变成 11. 元月,把小数点弄后面去了,如何修改才能正常显示1.1,并且把样式更改?

作者: phuai007   发布时间: 2011-10-21

var str = $(this).html().replace(/([^0-9\.]+)/gi, '');
var str1 = $(this).html().replace(/([0-9\.]+)+/gi, '');


楼主 试试

作者: calmcrime   发布时间: 2011-10-21


HTML code

<script src="jquery.js"></script>

<script>
//数字变色
$(function () {
            $("span").each(function () {
                $(this).html().replace(/(\d*)\.?(.*)/, '$1 $2');
                alert(RegExp.$1);alert(RegExp.$2);
                $(this).html(strdiv);
            });
        });
</script>


<span>100元/月</span>
<span>1.1 元/月</span>




值都取到了,样式自己设

作者: sk_up_dyy   发布时间: 2011-10-21

手快了,去掉 $(this).html(strdiv);
HTML code

<script src="jquery.js"></script>

<script>
//数字变色
$(function () {
            $("span").each(function () {
                $(this).html().replace(/(\d*)\.?(.*)/, '$1 $2');
                alert(RegExp.$1);alert(RegExp.$2);
            });
        });
</script>


<span>100元/月</span>
<span>1.1 元/月</span>

作者: sk_up_dyy   发布时间: 2011-10-21

JScript code
        $(function () {
            $(".info .info_content span[class='money']").html(function(index, txt){
                return txt.replace(/(\d+(?:\.\d+)?)(\D*)/, "<span style='color:red;font-size:16px;font-family:Adobe 黑体 Std R'>$1</span>" + "<span style='font-size:12px;'>$2</span>");
            });
        });

作者: axiheyhey   发布时间: 2011-10-21