469,573 Members | 1,415 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,573 developers. It's quick & easy.

On Javascript Replace Method.

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?

---------------------

Jul 29 '07 #1
9 3631
Mark Szlazak wrote:
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
Jul 29 '07 #2
On Jul 29, 5:48 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Mark Szlazak wrote:
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.

Jul 29 '07 #3
David Mark wrote:
Thomas 'PointedEars' Lahn <PointedE...@web.dewrote:
>Mark Szlazak wrote:
>>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
Jul 29 '07 #4
Thomas 'PointedEars' Lahn said the following on 7/29/2007 5:48 PM:

<snip>

He's baaaaaaaack. School must be out.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 30 '07 #5
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;
}
}
Jul 30 '07 #6
Please quote the minimum of what you are referring to:

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

Mark Szlazak wrote:
Thomas 'PointedEars' Lahn wrote:
>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.

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
Jul 30 '07 #7
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.

Jul 31 '07 #8
Mark Szlazak wrote:
>>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, <f8*******************@news.demon.co.uk>
Jul 31 '07 #9
On Jul 31, 2:28?am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Mark Szlazak wrote:
>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, <f806at$ail$1$8300d...@news.demon.co.uk>- 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]

Jul 31 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.