如何从文本框中更改正则表达式模式? [重复]

时间:2021-01-21 21:46:33

This question already has an answer here:

这个问题在这里已有答案:

How I could set the variable pat to act as variable patt1? I want write in the textbox just "abe" and change from var patt1 = /\b[abc]+\b/g; to var patt1 = /\b[abe]+\b/g;. Is that possible?

我如何设置变量pat作为变量patt1?我想在文本框中写“abe”并从var patt1 = / \ b [abc] + \ b / g更改; to var patt1 = / \ b [abe] + \ b / g;。那可能吗?

<html>
    <body onload="onload();">


    <input type="text" id="lol"/>
    <input type="button" VALUE="Resitve" onclick="myFunction();"/>

    <p id="alert"></p>

    <script>
    var pat;


    function myFunction() {
        pat = document.getElementById("lol").value;

        var str = "abc ab abe abeee";
        var patt1 = /\b[abc]+\b/g;

        var result = str.match(pat);
        document.getElementById("alert").innerHTML = result;
    }
    </script>

    </body>
    </html>

2 个解决方案

#1


0  

You can use the RegExp object:

您可以使用RegExp对象:

<html>
    <body onload="onload();">


    <input type="text" id="lol"/>
    <input type="button" VALUE="Resitve" onclick="myFunction();"/>

    <p id="alert"></p>

    <script>
    var pat;


    function myFunction() {
        pat = document.getElementById("lol").value;

        var str = "abc ab abe abeee";
        var patt1 = new RegExp("\\b[" + pat + "]+\\b","g");

        var result = patt1.exec(str);
        document.getElementById("alert").innerHTML = result;
    }
    </script>

    </body>
    </html>

Or you can simply make the pattern in your code as string:

或者您可以简单地将代码中的模式作为字符串:

<html>
    <body onload="onload();">


    <input type="text" id="lol"/>
    <input type="button" VALUE="Resitve" onclick="myFunction();"/>

    <p id="alert"></p>

    <script>
    var pat;


    function myFunction() {
    pat = document.getElementById("lol").value;

    var str = "abc ab abe abeee";
    var patt1 = "\\b[" + pat + "]+\\b";

    var result = str.match(patt1);
    document.getElementById("alert").innerHTML = result;
    }
    </script>

    </body>
    </html>

If you want to truly find all matches, you will need to call the exec function in a loop:

如果要真正找到所有匹配项,则需要在循环中调用exec函数:

<html>
    <body onload="onload();">


    <input type="text" id="lol"/>
    <input type="button" VALUE="Resitve" onclick="myFunction();"/>

    <p id="alert"></p>

    <script>
    var pat;


    function myFunction() {
        pat = document.getElementById("lol").value;

        var str = "abc ab abe abeee";
        var patt1 = new RegExp("\\b[" + pat + "]+\\b","g");

        var result;
        var display = document.getElementById("alert");
        display.innerHTML = "";
        while(result = patt1.exec(str)){
            display.innerHTML += result + "<br/>";
        }
    }
    </script>

    </body>
    </html>

#2


0  

From the String.prototype.match() docs

来自String.prototype.match()文档

str.match(regexp)

regexp

If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

如果传递了非RegExp对象obj,则使用新的RegExp(obj)将其隐式转换为RegExp。

So, passing your regex as a concatenated string should work fine. i.e., instead of this:

因此,将正则表达式作为连接字符串传递应该可以正常工作。即,而不是这个:

var patt1 = /\b[abc]+\b/g;

Use this:

var patt1 = "/\b[abc]+\b/g";

And amend that string as you see fit!

并根据需要修改该字符串!

#1


0  

You can use the RegExp object:

您可以使用RegExp对象:

<html>
    <body onload="onload();">


    <input type="text" id="lol"/>
    <input type="button" VALUE="Resitve" onclick="myFunction();"/>

    <p id="alert"></p>

    <script>
    var pat;


    function myFunction() {
        pat = document.getElementById("lol").value;

        var str = "abc ab abe abeee";
        var patt1 = new RegExp("\\b[" + pat + "]+\\b","g");

        var result = patt1.exec(str);
        document.getElementById("alert").innerHTML = result;
    }
    </script>

    </body>
    </html>

Or you can simply make the pattern in your code as string:

或者您可以简单地将代码中的模式作为字符串:

<html>
    <body onload="onload();">


    <input type="text" id="lol"/>
    <input type="button" VALUE="Resitve" onclick="myFunction();"/>

    <p id="alert"></p>

    <script>
    var pat;


    function myFunction() {
    pat = document.getElementById("lol").value;

    var str = "abc ab abe abeee";
    var patt1 = "\\b[" + pat + "]+\\b";

    var result = str.match(patt1);
    document.getElementById("alert").innerHTML = result;
    }
    </script>

    </body>
    </html>

If you want to truly find all matches, you will need to call the exec function in a loop:

如果要真正找到所有匹配项,则需要在循环中调用exec函数:

<html>
    <body onload="onload();">


    <input type="text" id="lol"/>
    <input type="button" VALUE="Resitve" onclick="myFunction();"/>

    <p id="alert"></p>

    <script>
    var pat;


    function myFunction() {
        pat = document.getElementById("lol").value;

        var str = "abc ab abe abeee";
        var patt1 = new RegExp("\\b[" + pat + "]+\\b","g");

        var result;
        var display = document.getElementById("alert");
        display.innerHTML = "";
        while(result = patt1.exec(str)){
            display.innerHTML += result + "<br/>";
        }
    }
    </script>

    </body>
    </html>

#2


0  

From the String.prototype.match() docs

来自String.prototype.match()文档

str.match(regexp)

regexp

If a non-RegExp object obj is passed, it is implicitly converted to a RegExp by using new RegExp(obj).

如果传递了非RegExp对象obj,则使用新的RegExp(obj)将其隐式转换为RegExp。

So, passing your regex as a concatenated string should work fine. i.e., instead of this:

因此,将正则表达式作为连接字符串传递应该可以正常工作。即,而不是这个:

var patt1 = /\b[abc]+\b/g;

Use this:

var patt1 = "/\b[abc]+\b/g";

And amend that string as you see fit!

并根据需要修改该字符串!