...

View Full Version : js lacks of regex funcs?



madson
05-08-2006, 09:55 PM
i'm attempting to get a letter appearing number in a string and have problems. firstly i tried with
filtered=document.formname.title.value.replace(/\s/,"");
words=filtered.length; but it didn't make the trick. as far as i know there's no a regex function that returns an array of matches or something else that helps my here. any ideas?
p.s. yes i know the 's' in the pattern must be escaped. actually it is but the php doesn't print it.

Beagle
05-08-2006, 09:57 PM
var arrayOfMatchesofNumbers = "a1b2c3d4e5f".match(/[0-9]/);

liorean
05-08-2006, 10:11 PM
Madson: There is a link to an article I wrote about regular expressions in JavaScript in my signature. You could always have a look at that.

Demonstrating a few of the JavaScript regex handling mechanisms:
var
r=/t/,
rg=/t/g,
s='test';
alert([
'r.test(s) : '+r.test(s),
'rg.test(s) : '+rg.test(s),
'r.exec(s) : '+r.exec(s),
'rg.exec(s) : '+rg.exec(s),
's.match(r) : '+s.match(r),
's.match(rg) : '+s.match(rg),
's.search(r) : '+s.search(r),
's.search(rg) : '+s.search(rg),
's.split(r) : '+s.split(r),
's.split(rg) : '+s.split(rg),
's.replace(r) : '+s.replace(r,'§'),
's.replace(rg) : '+s.replace(rg,'§')].join('\n'));

This will alert:
r.test(s) : true
rg.test(s) : true
r.exec(s) : t
rg.exec(s) : t
s.match(r) : t
s.match(rg) : t,t
s.search(r) : 0
s.search(rg) : 0
s.split(r) : ,es,
s.split(rg) : ,es,
s.replace(r) : §est
s.replace(rg) : §es§



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum