“与括号内的任何字符匹配的正则表达式”的更多帮助

时间:2018-07-23 10:05:59

标签: javascript regex

我问了一个相关的问题on this thread,这对我很有帮助。

我使用了以下代码:

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;
}

对于用户数组:“ Carin Nilsson-Thorell(CARIN.NILSSON-T),Maria Bengzon(MARIA.BENGZON),Therese Rick(THERESE.RICK),Inti van Eck(IPEINTECK)”

控制台日志显示:

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

这正是我所希望的。 但是,当用户数组只有一个用户时,“ Inti van Eck(IPEINTECK)” 控制台日志显示

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

所以看来它甚至都没有输入“ if”语句?怎么来的?

我什至尝试了https://regex101.com/,并且可以正常运行...

我也尝试了/(?<=()[^)] *(?==))/版本,但这对?引发了一些错误。 (忘记捕获该错误。现在尝试重现该错误,并引发其他错误-我将尽快添加错误文本...)

我仍在尝试学习有关JavaScript的所有知识,但我确实遇到了困难。

编辑

这是调用extractUsers函数的“父函数”:

    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);
    }
}

1 个答案:

答案 0 :(得分:1)

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!