简体   繁体   中英

Javascript syntax error caused by regex - specific to Internet Explorer(all versions)

I have a web application I ported from a Cordova project which works without problems on all common browsers except for IE. So far I have tested this application in Chrome, Chromium, Firefox, Safari and Edge.

The problem in Internet Explorer returns an error saying that "'submit' is undefined" whenever the following line is added to the code.

usr_id = /\[usr_id\] => (.*?)\n/gum.exec(response);

My guess is that the escape characters are somehow breaking the syntax but playing around with that didn't solve my problem.

The offending part in context below:

    function submit(){
        uname = $('#txt-uname').val();
        password = $('#txt-password').val();
        login(uname,password);
    }

    function login(tc,pass){
        $.ajax({
            type: 'POST',
            url:  webserviceURL+'login.php',
            data: {'tc':tc, 'password':pass},
            complete: function(r){
                response = r.responseText;

                if (response.indexOf("") > -1){
                    localStorage.setItem("login", "true");

                    usr_id = /\[usr_id\] => (.*?)\n/gum.exec(response);
                    localStorage.setItem("usr_id", 'test');

                }
            }
        });
    }

You cannot have u as the unicode flag in Internet Explorer. It is not supported yet. Please change your RegEx to:

usr_id = /\[usr_id\] => (.*?)\n/gm.exec(response);

Also, you can check the support by checking the response of:

RegExp.prototype.unicode

And if it supported for the browsers, use the u flag, as it breaks your original logic without that. With this change, at least, it works in Internet Explorer gracefully. This can be checked using:

(new RegExp()).unicode

It might return undefined in Internet Explorer, while false in other browsers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM