advanced regex : split problem in IE (vs moz)

B

brendan

Hello, I have been scripting for moz and then checking everything is working
in IE ... except I forgot to check at one point and have only just realised
that there is an error in blasted IE.
The below function [with a few snips] breaks up a search string using
[column][colon][text]
i.e. "title:Star Wars author:George Lucas date:1971";
should split to:

title=>star wars,
author=>george lucas
date=>1971

This works perfectly in mozilla but bugs out in explorer (results in 'george
lucas'=>'1971' ... given IE's crappy debugging I can't pinpoint the exact
problem
any ideas?


var search_text= "title:Star Wars author:George Lucas date:1971"; // random
bit of text here

thestring=search_text.replace(/^\s*|\s*$/g,""); //trim string
var regexp=/\s*(\w+):/ig;
var writestring=thestring.split(regexp);

for(var i=1;i<writestring.length;i+=2) {
j=i+1;

thevar=writestring;
theval=writestring[j];

alert(thevar+'=>'+theval); // do something with the vals (usually put
into array)
}
 
L

Lasse Reichstein Nielsen

brendan said:
Hello, I have been scripting for moz and then checking everything is working
in IE ... except I forgot to check at one point and have only just realised
that there is an error in blasted IE.

True. IE appears not to follow ECMAScript v.3's specification of
String.prototype.split wrt. capturing groups in the pattern.
(I didn't know about it either, before reading it just now :)
This works perfectly in mozilla but bugs out in explorer (results in 'george
lucas'=>'1971' ... given IE's crappy debugging I can't pinpoint the exact
problem

The problem is that the capturing parentheses in the pattern doesn't
do anything in IE, so only the content between the matches ends up in
the result array.
any ideas?

Try:
---
var match = search_text.match(/\w+:[\w\d\s]+(\s+|$)/g);
// or even /\w+:[^:]*(\s+|$)/g

for (var i = 0, n = match.length; i<n; i++) {
var p = match.replace(/\s+$/,"").split(":");
alert(p[0] + " => " + p[1]);
}
 
B

brendan

True. IE appears not to follow ECMAScript v.3's specification of
String.prototype.split wrt. capturing groups in the pattern. (I didn't
know about it either, before reading it just now :)

Brilliant, where'd you find that???

var match = search_text.match(/\w+:[\w\d\s]+(\s+|$)/g);
// or even /\w+:[^:]*(\s+|$)/g
for (var i = 0, n = match.length; i<n; i++) {
var p = match.replace(/\s+$/,"").split(":");

alert(p[0] + " => " + p[1]);

---

Excellent workaround! Thanks Lasse.

Regards,

Brendan.
 
L

Lasse Reichstein Nielsen

brendan said:
Brilliant, where'd you find that???

The spec (ECMA 262 v3) or that IE doesn't follow it (by inserting
an alert and seeing that the result of split wasn't the same as in
Opera and Firefox)? :)

/L
 
B

brendan

Lasse Reichstein Nielsen said:
The spec (ECMA 262 v3) or that IE doesn't follow it (by inserting
an alert and seeing that the result of split wasn't the same as in
Opera and Firefox)? :)

the fact that IE doesn't follow it (excepting for gut instinct of course)
 
B

brendan

Lasse Reichstein Nielsen said:

var match = search_text.match(/\w+:[\w\d\s]+(\s+|$)/g);
// or even /\w+:[^:]*(\s+|$)/g
for (var i = 0, n = match.length; i<n; i++) {
var p = match.replace(/\s+$/,"").split(":");

alert(p[0] + " => " + p[1]);

---

Excellent workaround! Thanks Lasse.


That said ... it chokes if there is a comma in the matched text
ie "title:Star Wars author:George Lucas date:1971 keywords:science,
fiction";

.... have to go home now unfortunately, will have a go at fixing in morning.
b.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top