内部javascript函数的正则表达式

时间:2013-01-08 11:17:30

标签: javascript regex

这是函数

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等的值。

1 个答案:

答案 0 :(得分: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]);

<强> As seen in this paste