Link to home
Start Free TrialLog in
Avatar of nap0leon
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:
test('variable1','a');

Open in new window


1 variable, 1 value, the value is a list (it is enclosed in double-quotes).
test('variable1','"a,b"');

Open in new window


2 variable, 2 values
test('variable1,variable2','a,b');

Open in new window


2 variable, 2 values, the first value is a list (it is enclosed in double-quotes).
test('variable1,variable2','"a,b",c');

Open in new window


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>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Proculopsis
Proculopsis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nap0leon
nap0leon

ASKER

Seems to work in all browsers I'm able to test.
Thanks!
@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