JavaScript - Need help with string manipulation
<p>say you have:</p>

<pre><code>var foo = "donut [$25]"
</code></pre>

<p>What would you need to do in order to delete everything between and including the <code>[ ]</code>.</p>

<p>so you get: <code>foo = "donut"</code> after the code is run.</p>

<p>So far I have tried most of the solutions below, but they all either do nothing or crash.
Maybe it's something with my code, please see below:</p>

<pre><code> $('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 &lt;= 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('&lt;span&gt;&lt;/span&gt;');

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

                var uop; //Unselected Option Price


                for (d = 0; d &lt;= 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 + "]");
                };





            });







        });


    };
</code></pre>
