[thelist] differences in javascript regexp among browsers?

liorean liorean at gmail.com
Thu Feb 17 04:31:45 CST 2005


On Wed, 16 Feb 2005 19:36:19 -0800, Diane Soini
<dianesoini at earthlink.net> wrote:
> I'm really getting bummed out by the differences in how browser do
> certain regular expression and string functions in javascript. I seem
> to get different results from regexp.exec(string) and
> string.split(regexp) in different browsers. Some browsers return more
> items in the array than others.

Often times this is because you're relying on features that are not
implemented in the browser that gives the least number of elements.

Regex in JavaScript is a pretty recent feature. Things such as the
multiline flag, non-greedy quantifiers, lookaheads, replace with
callback functions and correct splitting of strings when the patterns
use captured subpatterns were not supported in ie5 or nn4.
 
> Any suggestions on the best way to find and extract matches on strings,
> split strings, etc that don't cause unexpected problems?

Avoid the features I mentioned above. Always test in saf (or any other
KJS/KHTML browser), ie5, ie6w, moz or recent op before use. Of the
mentioned browsers, ie5 is the one that have most problems. Ie6w and
saf aren't problem free either, though. An example:

re      =   /t(e(?=s)).(t)/
ts      =   'test'
tx      =   'text'

1. re.exec(ts)
2. re.exec(tx)
3. ts.split(re)
4. tx.split(re)


Op8, Moz (these are unquestionably the best browsers at JavaScript):
1. ['test','e','t']
2. ['text','','']
3. ['','e','t','']
4. ['','','','xt']

Ie6w:
1. ['test','e','t']
2. ['text','','']
3. ['']
4. ['xt']

Saf:
1. ['test','e','t']
2. null
3. ['','']
4. ['text']

Ie5m
- Error, doesn't support the lookahead, so let's test without lookahead

re      =   /t(e).(t)/

1. ['test','e','t']
2. ['text','e','t']
3. []
4. []



Since you might find yourself interested in it, my article on regex in
JavaScript:
<uri:http://evolt.org/article/Regular_Expressions_in_JavaScript/17/36435/>
// David
-- 
David "liorean" Andersson
<uri:http://liorean.web-graphics.com/>


More information about the thelist mailing list