-1

say you have:

var foo = "donut [$25]"

What would you need to do in order to delete everything between and including the [ ].

so you get: foo = "donut" after the code is run.

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:

 $('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 + "]");
                };





            });







        });


    };
0

6 Answers 6

2

You can also use a regular expression, as Jon Martin pointed out:

var yum = "donut[$25]"; yum.replace(/[.*?]/, ""); // returns "donut"

Sign up to request clarification or add additional context in comments.

Comments

1

Alternatively:

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

foo = temp + temp2;

Comments

1

You can use regular expressions (the RegExp() object) to match strings.

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

The above will return an array of every item in [square brackets], in this case ["[$25]"].

To just get one result as a string, specify the first index like so:

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

The above will return "[$25]"

Edit: You know what? I completely misread which bit of the string you're after. This is what you're after:

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

Comments

0

How about simply;

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

>>donut

4 Comments

This won't work for strings that have characters after the ']'.
It will work for donut[$34]weqweqwe as it truncates at the 1st [ which seems reasonable for what appears to be structured data
He only wanted to delete everything between [..] inclusively. Your method will delete everything after the ].
var xtext = $(this).text().toString(); xtext.substr(0, xtext.indexOf("[")); - Does not seem to be doing anything ?
0

var begin = foo.search("["); var end = foo.search("]"); var result = foo.substr(0, begin) + foo.substr(end+1); //Combine anything before [ and after ]

Should be ok right?

Comments

0

Your question leaves unspecified the treatment of the spaces before the [ character, anything after the ], will your string ever contain a linefeed character, multiple occurrences of [..], leading or trailing spaces.

The following will replace all occurrences of 'spaces [ ... ] spaces' with a single space, then it trims the result to remove any leading/trailing spaces.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.