I’m having trouble with a regex in my web app. It works fine in Chrome, Firefox, Safari, and Edge. But in Internet Explorer, it’s causing a weird error.
The problem happens when I try to use this line:
userInfo = /\[user_info\] => (.*?)\n/gum.exec(serverResponse);
IE says ‘sendData’ is undefined. I think it might be the escape characters, but I’m not sure. I’ve tried different ways to write it, but no luck.
Here’s part of my code:
function sendData() {
username = $('#username-input').val();
pass = $('#password-input').val();
authenticateUser(username, pass);
}
function authenticateUser(id, pwd) {
$.ajax({
type: 'POST',
url: apiEndpoint + 'auth.php',
data: {'id': id, 'password': pwd},
complete: function(result) {
serverResponse = result.responseText;
if (serverResponse.includes('success')) {
localStorage.setItem('isLoggedIn', 'true');
userInfo = /\[user_info\] => (.*?)\n/gum.exec(serverResponse);
localStorage.setItem('userInfo', 'placeholder');
}
}
});
}
Any ideas why this is happening only in IE?
hey owen, ie can be a real pain! I’ve run into similar issues. try changing ur regex to use the older syntax: /[user_info] => (.*?)\n/gi the ‘u’ and ‘m’ flags might be causing trouble. also, double-check that ‘serverResponse’ is defined before the regex. hope this helps!
Have you considered using a regex testing tool specifically for IE? Sometimes IE’s regex engine behaves differently, and it’s not always obvious why. I’d suggest using something like RegexBuddy or RegexPal to test your pattern in an IE-compatible environment.
Another thing to check is whether ‘serverResponse’ is actually defined when you’re trying to use it. IE can be stricter about variable scoping. Make sure it’s accessible within the scope where you’re using the regex.
If all else fails, you might want to look into using a polyfill for modern regex features in IE. There are libraries available that can patch IE’s regex implementation to be more consistent with other browsers.
Remember, supporting IE can be a headache. If possible, consider dropping support for it if your user base doesn’t require it. Most modern businesses have moved on from IE.
I’ve encountered similar IE-specific regex issues in the past. One workaround that’s worked for me is using the RegExp constructor instead of the literal notation. Try this:
var regex = new RegExp('\\[user_info\\] => (.*?)\\n', 'gi');
userInfo = regex.exec(serverResponse);
This approach avoids potential parsing issues with certain flags in IE. Also, make sure ‘serverResponse’ is properly defined and contains the expected data. IE can be finicky about variable scoping, so double-check that it’s accessible within your function.
If that doesn’t solve it, you might need to use a polyfill for more advanced regex features in IE. Libraries like ‘xregexp’ can help bridge the gap between modern and older browsers.
Lastly, consider using a try-catch block around your regex operation to get more detailed error information in IE. This can help pinpoint the exact cause of the ‘undefined’ error you’re seeing.