473,553 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)(?:n ame1|name2|name 3)\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 3776
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)(?:n ame1|name2|name 3)\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...@we b.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)(?:n ame1|name2|name 3)\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...@we b.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.javas cript 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.substrin g(0,$.index) + map[$[0]] +
source.substrin g(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:
>Unfortunatel y, 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.substrin g(0,$.index) + map[$[0]] +
source.substrin g(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.substrin g(0,$.index) + map[$[0]] +
source.substrin g(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.substrin g(0,$.index) + map[$[0]] +
source.substr ing(rx.lastInde x);
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.substrin g(0,$.index) + map[$[0]]
+ source.substrin g(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.de mon.co.uk>
Jul 31 '07 #9
On Jul 31, 2:28?am, Thomas 'PointedEars' Lahn <PointedE...@we b.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.substrin g(0,$.index) + map[$[0]] +
source.substri ng(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.substrin g(0,$.index) + map[$[0]]
+ source.substrin g(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$8 300d...@news.de mon.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
7492
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7772
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7852
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6109
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5133
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3537
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1990
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
808
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.