Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: regexp help!

  1. #1
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question regexp help!

    Hello @all readers,

    i'm trying to extract some text out of an options-selectmenu.
    The text of these option menus are pulled out of a webdb.

    For each option there excist a main value,for each main value, there excist an aditional price,either a minus (-) or a plus (+) to the main price:

    main item: graphiccard price 200,-

    options:
    16 Mb (- 10)
    32 Mb (main price,no need to get aditional value)
    64 Mb (+ 50)
    128 Mb (+ 100)

    So if user choose 128 MB (+ 100)

    i require a var that say's : 128 MB
    a var that say's: +
    a var that say's : 100


    All selected options could contain many variants, it isnt pinned to the sample above,however,all options are formatted like this:

    someTextAndMabyNumbers_1 (aPlusOrMinusSign_2 AlwaysaNumber_3)


    I would apreciate to get a solution to this problem.
    I am not programming for living,the end result is for community.(oscommerce)
    To see a live version , ask me for the url (incase of seen as spam i not post until aprovement)


    Here is the code i pull out from the selectmenu,all alternative methodes instead of using regexp are much welcome.

    Code when clicked submit button and the regexp result var comes:
    Code:
    $("#submitme").live('click', function() {
        var mystringId = prodID;
        var addedOn = new Date();
    	
        $('select').each(function() {
            var selectId= $(this).attr("id");
            var selectedOption = $(this).find('option:selected');
            var selectedOptionValue = selectedOption.val();
    
            mystringId += '{' + selectId + '}' + selectedOptionValue;
        });
            db.transaction(function(tx) {
            tx.executeSql('SELECT COUNT(*) AS total FROM customers_basket WHERE products_unique_id ="'+ mystringId +'"', [], function (tx, results) {
            total = results.rows.item(0)['total'];
        
        if (total == 0){
        
        $('select').each(function() {
            
            var selectName= $(this).attr("name");
            var selectId= $(this).attr("id");
            var selectedOption = $(this).find('option:selected');
            var selectedOptionValue = selectedOption.val();
            var selectedOptionText = selectedOption.text();
    
            db.transaction(function(tx) {
    		tx.executeSql('INSERT INTO customers_basket_attributes (customers_id, products_id, products_unique_id, customers_basket_quantity, products_options_id, products_options_txt, products_options_value_id, products_options_value_txt) values ("1" , "'+prodID+'" , "'+mystringId+'" ,"1", "'+selectId+'" , "'+selectName+'" , "'+selectedOptionValue+'" , "'+selectedOptionText+'")');
            });
    		
        });
    	
        db.transaction(function(tx) {	
    	tx.executeSql('INSERT INTO customers_basket (customers_id, products_id, products_unique_id, customers_basket_quantity, final_price, customers_basket_date_added) values ("1" , "'+prodID+'" , "'+mystringId+'" , "1" , "0" , "'+addedOn+'")');
        });
    	
    	}else{
    	tx.executeSql('UPDATE customers_basket SET customers_basket_quantity = customers_basket_quantity + 1 WHERE products_unique_id ="'+ mystringId +'"');
    	tx.executeSql('UPDATE customers_basket_attributes SET customers_basket_quantity = customers_basket_quantity + 1 WHERE products_unique_id ="'+ mystringId +'"'); 
    	
    	}
    	});
    		});
    $('.ui-dialog').dialog('close');
    
    });
    Code where the option selectmenu is generated:

    option.text(val+ ' (' +prefix+ ' ' +price+ ') '); representing the required regexp / alternativecode

    Code:
        for(key in products_options_array)
            {
           
        var value=products_options_array[key];
    	    
    	    prodOpId = value['products_options_id'];
    	    prodOpName = value['products_options_name'];
            prodOpValueId = value['products_options_values_id'];
    	    prodOpValueName = value['products_options_values_name'];
    		prodOpValuePrice = value['options_values_price'];
    		prodOpValuePrefix = value['price_prefix'];
    	   
        var arr = [
            {
            optionname: prodOpName,
            optionvalue: prodOpValueName,
    		optionnameid: prodOpId,
    		optionvalueprice: prodOpValuePrice,
    		optionvaluepriceprefix: prodOpValuePrefix,
    		}
            ];
    
    
        for (var i = 0; i < arr.length; i++) {
        var r = arr[i];
    	//console.log(r);
            name    = r.optionname;
            val     = r.optionvalue;
    		nameid  = r.optionnameid;
    		valueid = r.optionvalueid;
    		price = r.optionvalueprice;
    		prefix = r.optionvaluepriceprefix;
        if ($("#prodAttributes_"+prodID+" select").is("[name*=" + name + "]")) {
    
    	var select = $("#prodAttributes_"+prodID).find("select[name*=" + name + "]");
        var option = $("<option>");
            option.val(valueid);
    		if (price == 0){
            option.text(val);
    		}else{
            option.text(val+ ' (' +prefix+ ' ' +price+ ') ');
    		}
    		select.append(option);
          } else {
      
        var select = $("<select>");
            select.attr("name", name);
            select.attr("id", nameid);
        var option = $("<option>");
            option.val(valueid);
    		if (price == 0){
            option.text(val);
    		}else{
            option.text(val+ ' (' +prefix+ ' ' +price+ ') ');
    		}
            select.append(option);
    	var label = $("<label>");
    		label.attr("class", "select");
            label.attr("for", name);
        
            
    		label.text(name);
    	    fieldset.append(select);
            myform.append(div);
            $(myform).appendTo("#prodAttributes_"+prodID);
    
            }
    	
            }
    	    }
            myform.append(myinput);	
            myform.trigger('create');
    Thank you verry much!!!!!!!!!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,193
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    You could use a regexp, but I wouldn't bother.

    I'd just do something like this:
    Code:
        var value = "128 Mb (+ 100)"; // this is an example only
    
        var temp = value.split("(");
        var memory = temp[0].replace(/\s+$/,""); // trim off space
        var plusminus = temp[0].charAt(0);
        var amount = temp[0].substring(1).replace(/^\s+/,"").replace(")","");
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    As it is to late now ( have to get up soon to work,yes on saturday 6 day a week),and didnt expected such a quick reply, i wont "THANK YOU" yet.But i realy apreciate your willing to help me out.
    I will ofcourse come here read again tomorrow after work and try out your code.

    However, i can see an "imperfection in it" , dont kill me yet.I somehow have the skill to "see" stuff and can tell abouth it,but it doesnt mean i know how to make things visable as how you did.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,193
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    The assumptions in that code:

    (1) You indeed use the format you said, so always have ( and ) and +/-:
    someTextAndMabyNumbers_1 (aPlusOrMinusSign_2 AlwaysaNumber_3)

    (2) you may or may not have a space before the (

    (3) you may or may not have a space after the +/- sign

    I don't think there are any other assumptions there.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Oct 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i just did a jsfiddle and am somehow confused by the results i got.

    please don get me wrong, i have no doubts on your profesionality.


    if take in consideration your skillz and mine i come to conclusion i do it wrong here:

    http://jsfiddle.net/whitehat/vrcXY/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •