MariaBenzin
MariaBenzin

Reputation: 29

A little more help for "Regex that match any character inside a parenthesis"

I asked a related question on this thread, and it helped me very much.

I used this code:

function extractUsers(usersArray) {
    var i; // loop counter
    for (i = 0; i < usersArray.length; i++) {

        if (/\(([^)]+)\)/.test(usersArray[i])) {
            // retrieves the user id from strings like "name (ID)"
            usersArray[i] = RegExp.$1;
            console.log('in the regexloop ' + i + ' of ' + usersArray.length + ' and test. current value: ' + usersArray[i]);
        }
        //console.log('the cleaned usersArray ' + usersArray);
    }
    return usersArray;
}

For the user array: "Carin Nilsson-Thorell (CARIN.NILSSON-T),Maria Bengzon (MARIA.BENGZON),Therese Rick (THERESE.RICK),Inti van Eck (IPEINTECK)"

the console log showed:

selected is Carin Nilsson-Thorell (CARIN.NILSSON-T),Maria Bengzon (MARIA.BENGZON),Therese Rick (THERESE.RICK),Inti van Eck (IPEINTECK)
in the regexloop 0 of 4 and test. current value: CARIN.NILSSON-T
in the regexloop 1 of 4 and test. current value: MARIA.BENGZON
in the regexloop 2 of 4 and test. current value: THERESE.RICK
in the regexloop 3 of 4 and test. current value: IPEINTECK
extracted selected is CARIN.NILSSON-T,MARIA.BENGZON,THERESE.RICK,IPEINTECK

Which is exactly what I hoped for. But when the user array was just one user, "Inti van Eck (IPEINTECK)" the console log showed

selected is Inti van Eck (IPEINTECK)
extracted selected is Inti van Eck (IPEINTECK)

So it seems it doesn't even enter the "if" statement? How come?

I even tried https://regex101.com/ and there it works ok...

I also tried the /(?<=()[^)]*(?=))/ version, but that threw some error about the ? (Forgot to capture that error. Trying to reproduce it now, throws me other errors - I'll add the error text as soon as I can...)

I am still trying to learn all I can about JavaScript, but I do stumble.

Edit

This is the "parent function" that calls the extractUsers function:

    function assignStepUsers() {
    var element = document.getElementById('mastercontrol.route.stepnumber');
    var stepNumber = element.value;
    console.log('running form assignStepUsers. stepNumber is ' + stepNumber);
    var selected = []; // array. holds the current value of users within a set of statements. set to empty for each stepNumber run.
    var i; //loop counter
    var pad = "00"; //size of id number
    var id; // id number of cause
    var counter; // what thing are we counting
    var selector; // what elements to select and count
    var nr; // how many things are launched (document.getElementById(counter).value)
    var things; // how many things in tota l (document.querySelectorAll(selector))
    var owner; // holds the current task owner within a set of statements
    var field; // the select field that holds all users of a step, with selected users highlighted
    var end; // length of field, for loop end.

    //var step2Users, step3Users, step4Users, step5Users, step6Users, step7Users, step8Users, step9Users, step10Users, step11Users, step12Users;
    // for preview, add "|| stepNumber == 0 || NaN(stepNumber)" after "stepNumber == 1" inside parenthesis
    if ((stepNumber == 1 ) && (document.getElementById('mastercontrol.set.CAPA_Owner').value !='' && document.getElementById('mastercontrol.set.CAPA_Coordinator').value != '')) { // removed || stepNumber == 0 || NaN(stepNumber), for troubleshooting/form preview only
        console.log('assignStepusers for Step 1 . selected CAPA Owner = ' + document.getElementById('mastercontrol.set.CAPA_Owner').value +  ' selected CAPA Coord = ' + document.getElementById('mastercontrol.set.CAPA_Coordinator').value);

        // assign QM and CAPA owner to step 2, 6, 8, 10, 12

        // find all content of the QM role field (QMOptions), then push values into array (QM)
        var QMOptions = document.getElementById('mastercontrol.role.QM').querySelectorAll('option');
        console.log('QMOptions length = ' + QMOptions.length);
        var QM = []; // array for all the QM users
        var oneQM; // the current QM user of the field
        for (i = 0; i < QMOptions.length; i++) {
            oneQM = QMOptions[i].value;
            QM.push(oneQM);
            console.log('QM is now ' + QM);
        }

        selected = QM;
        console.log('selected is ' + selected);

        var capaOwner = document.getElementById('mastercontrol.set.CAPA_Owner').value;
        selected.push(capaOwner);
        console.log('selected is ' + selected);

        extractUsers(selected);
        console.log('extracted selected is ' + selected);
        sortUsers(selected);
        console.log('sorted selected is ' + selected);

        setStepUsers('mastercontrol.route.stepusers.step2', selected);
        setStepUsers('mastercontrol.route.stepusers.step6', selected);
        setStepUsers('mastercontrol.route.stepusers.step8', selected);
        setStepUsers('mastercontrol.route.stepusers.step10', selected);
        setStepUsers('mastercontrol.route.stepusers.step12', selected);

        // assign ONLY capa owner to step 4 
        selected = capaOwner; // QM users are removed.
        console.log('selected is ' + selected); 
        extractUsers(selected);
        // no need for sortUsers, only one in array.
        console.log('extracted selected is ' + selected);
        setStepUsers('mastercontrol.route.stepusers.step4', selected);

        //assign CAPA Coordinator to 3,5,7,9,11
        var capaCoord = document.getElementById('mastercontrol.set.CAPA_Coordinator').value;
        selected = capaCoord;
        console.log('selected is ' + selected);
        extractUsers(selected);
        // no need for sortUsers, only one in array.
        console.log('extracted selected is ' + selected);
        setStepUsers('mastercontrol.route.stepusers.step3', selected);
        setStepUsers('mastercontrol.route.stepusers.step5', selected);
        setStepUsers('mastercontrol.route.stepusers.step7', selected);
        setStepUsers('mastercontrol.route.stepusers.step9', selected);
        setStepUsers('mastercontrol.route.stepusers.step11', selected);
    }
}

Upvotes: 1

Views: 111

Answers (1)

MariaBenzin
MariaBenzin

Reputation: 29

It was the arguments passed to the function, more precisely here:

    // assign ONLY capa owner to step 4 
    selected = capaOwner; // QM users are removed. //<---- this was wrong!
    console.log('selected is ' + selected); 
    extractUsers(selected);
    // no need for sortUsers, only one in array.
    console.log('extracted selected is ' + selected);
    setStepUsers('mastercontrol.route.stepusers.step4', selected);

    //assign CAPA Coordinator to 3,5,7,9,11
    var capaCoord = document.getElementById('mastercontrol.set.CAPA_Coordinator').value;
    selected = capaCoord; //<---- this was wrong!
    console.log('selected is ' + selected);
    extractUsers(selected);

When I used this instead:

    // assign ONLY capa owner to step 4 
    selected = []; // empty the variable array again.
    selected.push(capaOwner);  // just add the capaOwner, which was defined above, to the array. (because having just selected = capaOwner failed.)
    console.log('selected is ' + selected); 
    extractUsers(selected);
    // no need for sortUsers, only one in array.
    console.log('extracted selected is ' + selected);
    setStepUsers('mastercontrol.route.stepusers.step4', selected);

    //assign CAPA Coordinator to 3,5,7,9,11
    var capaCoord = document.getElementById('mastercontrol.set.CAPA_Coordinator').value;
    //selected = capaCoord;
    selected = []; // empty the variable array again.
    selected.push(capaCoord);  // just add the capaCoord, which was defined above, to the array. (because having just selected = capaCoord failed.)
    console.log('selected is ' + selected);
    extractUsers(selected);

it went just as I hoped!

Today's lesson: if you want an array to stay an array, don't simply assign a string value to it. Use the .push method.

Thanks all!

Upvotes: 1

Related Questions