J
Jason S
Is there a way to get the position of multiple substrings that match a
regexp without using closures? match() returns the substrings
themselves, not the positions, and search() seems to only return the
first position. Here's what seems to work (under Shanti Rao's jsdb.exe
shell) but I get a bit nervous about using closures...
function bind_cache(x) { return function (subs,ofs)
{ x.push([ofs,subs.length]); return ""; }}
x=[];
f=bind_cache(x);
s='The quick brown fox jumped over the lazy dogs';
junk=s.replace(/\w*o\w*/g, f);
for (i = 0; i < x.length; ++i) writeln(s.slice(x[0],x[0]+x
[1]));
output (all the words with an 'o'):
brown
fox
over
dogs
regexp without using closures? match() returns the substrings
themselves, not the positions, and search() seems to only return the
first position. Here's what seems to work (under Shanti Rao's jsdb.exe
shell) but I get a bit nervous about using closures...
function bind_cache(x) { return function (subs,ofs)
{ x.push([ofs,subs.length]); return ""; }}
x=[];
f=bind_cache(x);
s='The quick brown fox jumped over the lazy dogs';
junk=s.replace(/\w*o\w*/g, f);
for (i = 0; i < x.length; ++i) writeln(s.slice(x[0],x[0]+x
[1]));
output (all the words with an 'o'):
brown
fox
over
dogs