Different regular expression splitting in Firefox/IE

E

Eric McGraw

In Firefox, as in most regular expression engines, the split function
works like this:
"His name is <name>. His email address is <email>.".split(/<(.*?)>/)
returns ["His name is ", "name", ". His email address is ", "email",
"."]

so that the text pattern in the parentheses is included in the result.

In Internet Explorer, however, the result is different:
"His name is <name>. His email address is <email>.".split(/<(.*?)>/)
returns ["His name is ", ". His email address is ", "."]

I would like to know if there is a way to get IE to include the text in
parentheses.


Thanks a lot,
Eric
 
E

Eric McGraw

I found a rather lame workaround:
"His name is <name>. His email address is <email>.".replace(/<(.*?)>/g, "[#SPLIT#]$1[#SPLIT#]").split('[#SPLIT#]')
If you can come up with anything better, please share it.

Eric
 
R

RobG

Eric McGraw said on 18/04/2006 2:43 PM AEST:
In Firefox, as in most regular expression engines, the split function
works like this:

"His name is <name>. His email address is <email>.".split(/<(.*?)>/)

returns ["His name is ", "name", ". His email address is ", "email",
"."]

so that the text pattern in the parentheses is included in the result.

In Internet Explorer, however, the result is different:

"His name is <name>. His email address is <email>.".split(/<(.*?)>/)

returns ["His name is ", ". His email address is ", "."]

I would like to know if there is a way to get IE to include the text in
parentheses.


.split(/[<>]/);
 
E

Eric McGraw

Thanks, but i would actually like to be able to filter what characters
can be in the brackets. Sorry for an incomplete example.

Eric
 
L

Lasse Reichstein Nielsen

Eric McGraw said:
I found a rather lame workaround:
"His name is <name>. His email address is <email>.".replace(/<(.*?)>/g, "[#SPLIT#]$1[#SPLIT#]").split('[#SPLIT#]')
If you can come up with anything better, please share it.

It's a know defect in IE, where it doesn't follow the ECMAScript standard.

I don't think it's necessarily *that* lame a solution :)

But let's see if replace can't be used for something funny:

function mySplit(string, regexp) {
var offset = 0;
var collection = [];
string.replace(regexp, function(m,g,i,s) {
collection.push(string.substring(offset, i), g);
offset = i + m.length;
});
collection.push(string.substring(offset));
return collection;
}

And then
mySplit("His name is <name>. His email address is <email>.", /<(.*?)>/g)
returns ["His name is ", "name", ". His email address is ", "email", "."]
as it should (remember the "g" after the regexp literal if you want to
match more than once).

/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Eric McGraw said:
I found a rather lame workaround:
"His name is <name>. His email address is
<email>.".replace(/<(.*?)>/g, "[#SPLIT#]$1[#SPLIT#]").split('[#SPLIT#]')
If you can come up with anything better, please share it.

It's a know defect in IE, where it doesn't follow the ECMAScript standard.

One can rightfully argue that an implementation that does not follow the
ECMAScript specification to the letter is not displaying a defect but is
a conforming implementation of ECMAScript, as different behavior and
language extensions are specified by ECMAScript to be conforming. Besides,
it is not IE that is the issue here, but Microsoft JScript.


PointedEars
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
One can rightfully argue that an implementation that does not follow the
ECMAScript specification to the letter is not displaying a defect but is
a conforming implementation of ECMAScript, as different behavior and
language extensions are specified by ECMAScript to be conforming.

The allowances for conforming implementations in ECMAScript standard
3rd Ed. section 2 (Conformance) allows additional features, but does
not allow different semantics of standard features. It must satisfy:

| A conforming implementation of ECMAScript must provide and support
| all the types, values, objects, properties, functions, and program
| syntax and semantics described in this specification.

JScript in IE does not implement the semantics of the "split" method
on String.prototype, and as such is not a conforming implementation.

It's close enough to being conforming that it's usually safe to assume
conformance, and then memorize the exceptions (this is one, array
literal elisions is another that I can remember).
Besides, it is not IE that is the issue here, but Microsoft JScript.

If might be an issue in other uses of JScript too. I am not familiar
with these. It is an issue with the JScript implementation used by IE
by default, which is why I allow myself to call it a problem in IE.

/L
 

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

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top