Mark Szlazak wrote:
Quote:
I don't think this is "do-able" but thought I'd better check. Say I
want to replace certain names in some source code as long as they are
not properties (dot properties) of objects.
There is no such thing as a "dot property". The lookup (dot) operator
is merely one of two ways to refer to object properties in ECMAScript
implementations.
Quote:
I could use a regular expression like:
>
rx = /(?:(\.)|\b)(?:name1|name2|name3)\b/g;
>
map = [];
map["name1"] = "a";
map["name2"] = "b";
map["name3"] = "c";
`name1', `name2', and `name3' will all be properties of the Array object
refered to by `map'; _not_ keys for elements of the array it
encapsulates. You have used the bracket property accessor syntax as
opposed to the dot property accessor syntax. The following is
semantical identical because all property names here are identifiers.
map = [];
map.name1 = "a";
map.name2 = "b";
map.name3 = "c";
Since no array-related properties are used whatsoever, complexity can be
reduced with
map = {};
or (downwards compatible):
map = new Object();
The rx and map variables, however, should be declared:
var rx = ...;
var map = [];
Quote:
Dot properties like .name1 are not replaced by anything new and they
need to be "skipped" over by this regular expression but other name1
identifiers need replacement with "a".
>
One problem with this approach is that dot properties like .name1 are
replaced by themselves and this is just unnecessary work.
Why are you matching against them in the first place? No match -- no
callback call.
source = source.replace(
/\b(name1|name2|name3)\b/g,
function (s, p1)
{
return (typeof map[p1] != "undefined"
? map[p1]
: p1);
}
});
But why all this replacing anyway?
PointedEars