javascript - JS String.replace can be tempremental with Safari -
the backstory:
this works fine in browsers except safari (5.0.1)
var chunk = arr[i]; chunk = chunk.replace('$', '\\$'); var = eval('message.match(/' + chunk + '/gi);'); if(a instanceof array) symbol = symbol.concat(a);
so modified following:
var chunk = string(arr[i]); chunk = chunk.replace('$', '\\$'); var = eval('message.match(/' + chunk + '/gi);'); if(a instanceof array) symbol = symbol.concat(a);
which made safari bit more happy throwing ambiguous error (fun!)
i've found solution , i'm posting below in case other fellow scripters run problem.
i couldn't reproduce problem because i'm not sure what's value of chunk
.
anyway, 1 more example of unnecessary usage of eval
, can use regexp
constructor build regexp
object string, example:
var re = /foo/gi;
is equivalent creating object in runtime regexp
constructor:
var re = new regexp('foo', 'gi');
applying code:
var chunk = string(arr[i]); // use string() if not sure if arr[i] // aware following line replacing first $ // char , may want scape other meta-characters: chunk = chunk.replace('$', '\\$'); var = message.match(new regexp(chunk,'gi')); if(a instanceof array) symbol = symbol.concat(a);
Comments
Post a Comment