...

View Full Version : regexp help!



wHiTeHaT
01-07-2012, 01:16 AM
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:

$("#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


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!!!!!!!!!

Old Pedant
01-07-2012, 01:26 AM
You could use a regexp, but I wouldn't bother.

I'd just do something like this:


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(")","");

wHiTeHaT
01-07-2012, 01:34 AM
As it is to late now ( have to get up soon to work,yes on saturday :eek: 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.

Old Pedant
01-07-2012, 01:44 AM
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.

wHiTeHaT
01-07-2012, 01:53 AM
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/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum