Converting user input string to regular expression

前端 未结 11 1953
无人及你
无人及你 2020-11-22 14:18

I am designing a regular expression tester in HTML and JavaScript. The user will enter a regex, a string, and choose the function they want to test with (e.g. search, match,

11条回答
  •  一个人的身影
    2020-11-22 14:32

    I use eval to solve this problem.

    For example:

        function regex_exec() {
    
            // Important! Like @Samuel Faure mentioned, Eval on user input is a crazy security risk, so before use this method, please take care of the security risk. 
            var regex = $("#regex").val();
    
            // eval()
            var patt = eval(userInput);
    
            $("#result").val(patt.exec($("#textContent").val()));
        }
    

提交回复
热议问题