473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3760
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.