On Javascript Replace Method.

M

Mark Szlazak

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. I could use a regular
expression like:

rx = /(?:(\.)|\b)(?:name1|name2|name3)\b/g;

map = [];
map["name1"] = "a";
map["name2"] = "b";
map["name3"] = "c";

source = source.replace(rx, function ($0, $1) {return $1?
$0:map[$0]});

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. Something
like a "false" return to skip replacement would be nice but the
following doesn't work.

source = source.replace(rx, function ($0, $1) {return $1?
false:map[$0]});

There are other ways to get around this by using something else
besides replace() but I wanted to see if it could be done with the
replace() method.

Is this possible?

---------------------
 
T

Thomas 'PointedEars' Lahn

Mark said:
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.
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 = [];
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
 
D

David Mark

Mark said:
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.
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 = [];
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);
}
});

You've got an extra closing curly bracket. It also doesn't work. I
was testing with a similar syntax:

map = {};
map["name1"] = "a";
map["name2"] = "b";
map["name3"] = "c";

source = 'name1 name2 name3 test.name1';
source = source.replace(
/\b(name1|name2|name3)\b/g,
function (s, p1) { return map[p1] || p1 } // assumes map will not
contain empty strings
);

This one (as well as yours) returns "a b c test.a"

I played around with a few variations on this theme and couldn't come
up with anything that avoided the redundant replacements.
 
T

Thomas 'PointedEars' Lahn

David said:
Thomas 'PointedEars' Lahn said:
Mark said:
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);
}
});

You've got an extra closing curly bracket.
Correct.

It also doesn't work.

That is also correct, I had merely showed that it is very easy to avoid
unnessary replacements with this approach if you simply don't match
against the corresponding strings. I overlooked, however, that it would
match too much, then. Sorry for that.

Unfortunately, ECMAScript RegExp does not support negative lookbehind
(yet), so it would seem one has to live with the additional matches (and
replacements) caused by its emulation (?:(\.)|\b) as used by the OP.


PointedEars
 
M

Mark Szlazak

It's to bad that replace() doesn't have something like this since it
would be more efficient that using exec() and some script to skip some
things and replace others like in the following:

while ($ = rx.exec(source)) {
if (!$[1]) {
source = source.substring(0,$.index) + map[$[0]] +
source.substring(rx.lastIndex);
rx.lastIndex = $.index + map[$[0]].length;
}
}
 
T

Thomas 'PointedEars' Lahn

Please quote the minimum of what you are referring to:

http://netmeister.org/news/learn2quote.html

Mark said:
It's to bad that replace() doesn't have something like this since it
would be more efficient that using exec() and some script to skip some
things and replace others like in the following:

while ($ = rx.exec(source)) {

The $ prefix should be used for machine-generated identifiers only.
if (!$[1]) {
source = source.substring(0,$.index) + map[$[0]] +
source.substring(rx.lastIndex);
rx.lastIndex = $.index + map[$[0]].length;
}
}

It would be more efficient if you used String::replace() on the
substring matched with RegExp::exec() before setting rx.lastIndex.


PointedEars
 
M

Mark Szlazak

It's to bad that replace() doesn't have something like this since it
would be more efficient that using exec() and some script to skip some
things and replace others like in the following:
while ($ = rx.exec(source)) {

if (!$[1]) {
source = source.substring(0,$.index) + map[$[0]] +
source.substring(rx.lastIndex);
rx.lastIndex = $.index + map[$[0]].length;
}
}

It would be more efficient if you used String::replace() on the
substring matched with RegExp::exec() before setting rx.lastIndex.

PointedEars

I'm not quite sure what you mean by this but if it's what I suspect
then this does not even work.
 
T

Thomas 'PointedEars' Lahn

Mark said:
It's to bad that replace() doesn't have something like this since it
would be more efficient that using exec() and some script to skip some
things and replace others like in the following:
while ($ = rx.exec(source)) {
if (!$[1]) {
source = source.substring(0,$.index) + map[$[0]] +
source.substring(rx.lastIndex);
rx.lastIndex = $.index + map[$[0]].length;
}
}
It would be more efficient if you used String::replace() on the
substring matched with RegExp::exec() before setting rx.lastIndex.
[...]

I'm not quite sure what you mean by this

Instead of

source = source.substring(0,$.index) + map[$[0]]
+ source.substring(rx.lastIndex);

you could write

var rx2 = new RegExp($[0]);
rx2.lastIndex = $.index;
source = source.replace(rx2, map[$[0]]);
but if it's what I suspect then this does not even work.

WFM :)


PointedEars
 
M

Mark Szlazak

Mark said:
It's to bad that replace() doesn't have something like this since it
would be more efficient that using exec() and some script to skip some
things and replace others like in the following:
while ($ = rx.exec(source)) {
if (!$[1]) {
source = source.substring(0,$.index) + map[$[0]] +
source.substring(rx.lastIndex);
rx.lastIndex = $.index + map[$[0]].length;
}
}
It would be more efficient if you used String::replace() on the
substring matched with RegExp::exec() before setting rx.lastIndex.
[...]
I'm not quite sure what you mean by this

Instead of

source = source.substring(0,$.index) + map[$[0]]
+ source.substring(rx.lastIndex);

you could write

var rx2 = new RegExp($[0]);
rx2.lastIndex = $.index;
source = source.replace(rx2, map[$[0]]);
but if it's what I suspect then this does not even work.

WFM :)

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not the
best source of advice on designing systems that use javascript.
-- Richard Cornford, <[email protected]>- Hide quoted text -

- Show quoted text -

I believe this doesn't work for a couple of reasons. The replace()
method will reset it's search to start at position 0, not position
$.index. Since it resets to position 0, this will cause it to match
$[0]'s that are preceeded by dot, .$[0]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top