473,386 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

advanced regex : split problem in IE (vs moz)

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[i];
theval=writestring[j];

alert(thevar+'=>'+theval); // do something with the vals (usually put
into array)
}
Sep 8 '05 #1
6 3618
"brendan" <br*************@srl.cam.ac.uk> writes:
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[i].replace(/\s+$/,"").split(":");
alert(p[0] + " => " + p[1]);
}
---
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 8 '05 #2


"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:<y8**********@hotpop.com>...
"brendan" <br*************@srl.cam.ac.uk> writes:
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 :)
Brilliant, where'd you find that???
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[i].replace(/\s+$/,"").split(":"); alert(p[0] + " => " + p[1]); } ---


Excellent workaround! Thanks Lasse.

Regards,

Brendan.
Sep 8 '05 #3
"brendan" <br*************@srl.cam.ac.uk> writes:
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:<y8**********@hotpop.com>...

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???


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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 8 '05 #4

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message

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)? :)


the fact that IE doesn't follow it (excepting for gut instinct of course)
'Faith without judgement merely degrades the spirit divine.'

Sep 8 '05 #5
> "Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:<y8**********@hotpop.com>...
"brendan" <br*************@srl.cam.ac.uk> writes:

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[i].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.
Sep 8 '05 #6
"brendan" <br*************@srl.cam.ac.uk> writes:
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:<y8**********@hotpop.com>...
var match = search_text.match(/\w+:[\w\d\s]+(\s+|$)/g);
// or even /\w+:[^:]*(\s+|$)/g
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";


Use the alternative regexp then. It accepts anything in the text that isn't
a colon.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 8 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Frank Oquendo | last post by:
I have the following code: string pattern = @"(\{)|(})|(\()|(\))|(\)|(\^)|(\*)|(/)|(-)|(\+)|(%)"; Regex regex = new Regex(pattern); string input = "QTY * ESTIMATED COST + 2"; string tokens =...
7
by: William Stacey [MVP] | last post by:
Would like help with a (I think) a common regex split example. Thanks for your example in advance. Cheers! Source Data Example: one "two three" four Optional, but would also like to...
5
by: Jianwei Sun | last post by:
string sTest="TEST1||TEST2"; string asTest =Regex.Split(sTest, "||" ); I want to get an array with two elements TEST1 and TEST2, but it returs every char inside the sTest as a seperate array...
3
by: Rico | last post by:
If there are consecutive occurrences of characters from the given delimiter, String.Split() and Regex.Split() produce an empty string as the token that's between such consecutive occurrences. It...
10
by: Claud Balls | last post by:
I am splitting large files based on a text delimeter, but I don't want the delimeter left out of the string. For example if I had a string "NAME: Bill TOWN: Helena NAME: Frank TOWN: Helena" I...
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
7
by: Jordi Rico | last post by:
Hi, I know I can split a string into an array doing this: Dim s As String()=Regex.Split("One-Two-Three","-") So I would have: s(0)="One" s(1)="Two"
1
by: mad.scientist.jr | last post by:
I am working in C# ASP.NET framework 1.1 and for some reason Regex.Split isn't working as expected. When trying to split a string, Split is returning an array with the entire string in element ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.