JavaScript - 需要有关字符串操作的帮助

时间:2011-08-16 15:07:19

标签: javascript

说你有:

var foo = "donut [$25]"

为了删除[ ]之间的所有内容,您需要做什么。

所以在代码运行后得到foo = "donut"

到目前为止,我已尝试过以下大多数解决方案,但它们都无所事事或崩溃。 也许这与我的代码有关,请参见下文:

 $('select').change(function () { OnSuccess(mydata); });


    function OnSuccess(data) {

        var total = 0;

        $('select').each(function () {


            var sov = parseInt($(this).find('option:selected').attr('value')) || 0; //Selected option value

            var sop; //Selected Option Price


            for (i = 0; i <= data.length; i++) {


                if (data[i].partid == sov) {

                    sop = data[i].price;
                    total += sop;
                    $('#totalprice').html(total);
                    break;
                }


            };








            //debugger;
            $(this).find('option').each(function () {

                // $(this).append('<span></span>');

                var uov = parseInt($(this).attr('value')) || 0; //Unselected option value

                var uop; //Unselected Option Price


                for (d = 0; d <= data.length; d++) {


                    if (data[d].partid == uov) {

                        uop = data[d].price;
                        break;
                    }

                }
                //debugger;
                var newtext = uop - sop;
                //{ newtext = "" };
                //if (newtext = 0) { newtext.toString; newtext = ""; };


                //debugger;
                var xtext = $(this).text().toString();

                //if (xtext.match(/\[.*\]/) != null) {
                    xtext.replace(/\s*\[[\s\S]*?\]\s*/g, '').trim();

                //}

                //                var temp = xtext.split('[')[0];
                //                var temp2 = xtext.split(']')[1];

                //                resultx = temp + temp2;

                if (newtext != 0) {


                    //xtext.replace(/[.*?]/, "");

                    $(this).attr("text", xtext + " " + "[" + "$" + newtext + "]");
                };





            });







        });


    };

6 个答案:

答案 0 :(得分:3)

你也可以使用正则表达式,正如Jon Martin指出的那样:

var yum =“donut [$ 25]”;   yum.replace(/ [。*?] /,“”); //返回“donut”

答案 1 :(得分:1)

可替换地:

var temp = foo.split('[')[0];
var temp2 = foo.split(']')[1];

foo = temp + temp2;

答案 2 :(得分:1)

您可以使用正则表达式(RegExp()对象)来匹配字符串。

var foo = "donut[$25]";
foo.match(/\[.*\]/);

上面将返回[方括号]中每个项目的数组,在本例中为["[$25]"]

要将一个结果作为字符串获取,请指定第一个索引,如下所示:

foo.match(/\[.*\]/)[0];

以上内容将返回"[$25]"

编辑:你知道吗?我完全误读了你所追求的那个字符串。这就是你要追求的目标:

var foo = "donut[$25]";
foo.match(/\w*/)[0];

答案 3 :(得分:0)

简单来说;

var yum = "donut[$25]";
print( yum.substr(0, yum.indexOf("[")) );

>>donut

答案 4 :(得分:0)

var begin = foo.search(“[”); var end = foo.search(“]”); var result = foo.substr(0,begin)+ foo.substr(end + 1); //在[及之后]

之前合并任何东西

应该没事吧?

答案 5 :(得分:0)

您的问题未指定在[字符,后面的任何内容]之前对空格的处理,您的字符串是否包含换行符,多次出现[..],前导或尾随空格。

以下内容将用一个空格替换所有出现的'空格 [...] 空格',然后修剪结果以删除任何前导/尾随空格

   v.replace (/\s*\[[\s\S]*?\]\s*/g, ' ').trim ();
相关问题