-1

这是功能

function getChecked(button, form)
{
    var name;
    for (i = 0; i < document.forms['CheckForm'].list.length; i++) {
        name = "Check" + (i + 0);
        if (document.forms['CheckForm'].list[i].checked == true) {
            if (name == "Check0")
                form.Check0.value = "2337667";
            else if (name == "Check1")
                form.Check1.value = "2335765";
            else if (name == "Check2")
                form.Check2.value = "2332651";
            else if (name == "Check3")
                form.Check3.value = "2328582";
        }

如何编写一个正则表达式来单独获取值的值form.check0.value,如果可能的话,获取form.check0.valueform.check1.value等的值。

4

1 回答 1

1

您可能正在寻找类似这样的正则表达式:

/\d{2,}/g

例子:

var s = 'function getChecked(button, form){\n    var name;\n    for (i = 0; i < document.forms["CheckForm"].list.length; i++) {\n        name = "Check" + (i + 0);\n        if (document.forms["CheckForm"].list[i].checked == true) {\n            if (name == "Check0")\n                form.Check0.value = "2337667";\n            else if (name == "Check1")\n                form.Check1.value = "2335765";\n            else if (name == "Check2")\n                form.Check2.value = "2332651";\n            else if (name == "Check3")\n                form.Check3.value = "2328582";\n        }\n    }\n}';
console.log(s.match(/\d{2,}/g));

输出:

["2337667", "2335765", "2332651", "2328582"];

至于@Daverandom 提供的那个 PHP 示例,可以大大缩短:

preg_match_all('/\d{2,}/', $str, $matches); // Get all numbers that are at least 2 characters long.
print_r($matches[0]);

如本贴所示

于 2013-01-08T11:33:38.873 回答