nap0leon
asked on
regular expression broken in IE, works in FireFox and Chrome
The following code works in FireFox and Chrome, but fails to perform as expected in IE.
The idea is that I pass in two parameters to a function. Each of these parameters may be an single value or a list.
Examples:
1 variable, 1 value:
1 variable, 1 value, the value is a list (it is enclosed in double-quotes).
2 variable, 2 values
2 variable, 2 values, the first value is a list (it is enclosed in double-quotes).
HTML test page below - all four tests should result in "OK", but, in IE only (IE8 in Quirks, IE7 Standards, and IE8 Standards), when processing the value "a,b", it says splitArray(regExp) is length 0 instead of 1. In the third test, ( "a,b",c ), you'll see that the "a,b" is totally ignored and it returns only c as a value.
The idea is that I pass in two parameters to a function. Each of these parameters may be an single value or a list.
Examples:
1 variable, 1 value:
test('variable1','a');
1 variable, 1 value, the value is a list (it is enclosed in double-quotes).
test('variable1','"a,b"');
2 variable, 2 values
test('variable1,variable2','a,b');
2 variable, 2 values, the first value is a list (it is enclosed in double-quotes).
test('variable1,variable2','"a,b",c');
HTML test page below - all four tests should result in "OK", but, in IE only (IE8 in Quirks, IE7 Standards, and IE8 Standards), when processing the value "a,b", it says splitArray(regExp) is length 0 instead of 1. In the third test, ( "a,b",c ), you'll see that the "a,b" is totally ignored and it returns only c as a value.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>
<script type="text/javascript">
function test(a,b){
var variables = parseCommaList (a)
var values = parseCommaList (b)
var msg= '';
msg += 'variables length='+variables.length;
msg += '\nvalues length='+values.length;
if (variables.length==values.length) { msg+='\nOK!'; }else{ msg+='\nFailed!'; }
alert(msg)
}
function parseCommaList (list) {
"use strict";
var regExp,
returnArray,
splitArray;
if (typeof list !== 'string') {
alert('"list" not a string in parseCommaList');
}
regExp = /((?:\"[\s\S]*?\")|(?:'[\s\S]*?'))|(?:\,)/;
if (list.match(/\"/g) && list.match(/\"/g).length%2===1) {
alert('error mismatched double quotes on: >'+list+'<')
}
if (list.match(/'/g) && list.match(/'/g).length%2===1) {
alert('error mismatched single quotes on: >'+list+'<')
}
alert('processing: '+list)
returnArray = new Array();
splitArray = list.split(regExp);
regExp = /(?:^(\s*)\"([\s\S]*)\"(\s*)$)|(?:^(\s*)'([\s\S]*)'(\s*)$)/;
for (var i = 0; i < splitArray.length; i++) {
if (splitArray[i] != undefined) {
if (splitArray[i] !== "" || i <= 0 || splitArray[i - 1] == undefined || !(splitArray[i - 1].match(regExp))) {
(splitArray[i] !== "" || i >= splitArray.length || splitArray[i + 1] == undefined || !(splitArray[i + 1].match(regExp)))?returnArray.push(splitArray[i].replace(regExp, "$1$2$3$4$5$6")):'';
} else {
}
} else {
}
}
for (i=0;i<returnArray.length;i++){
alert(returnArray[i])
}
return returnArray;
}
</script>
</head>
<body>
<script type="text/javascript">
test('variable1','a');
test('variable1','"a,b"');
test('variable1,variable2','a,b');
test('variable1,variable2','"a,b",c');
</script>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@Proculopsis:
Found a couple issues with the solution:
1- '"a,b",c"' returns "a,b" as the first value instead of just a,b
2- does not support double-quotes on the outside, as in "'a,b',c".
Opened a new question:
https://www.experts-exchange.com/questions/27760113/regular-expression-broken-in-IE-works-in-FireFox-and-Chrome.html
Found a couple issues with the solution:
1- '"a,b",c"' returns "a,b" as the first value instead of just a,b
2- does not support double-quotes on the outside, as in "'a,b',c".
Opened a new question:
https://www.experts-exchange.com/questions/27760113/regular-expression-broken-in-IE-works-in-FireFox-and-Chrome.html
ASKER
Thanks!