Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / July 2007



Tip: Looking for answers? Try searching our database.

On Javascript Replace Method.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark Szlazak - 29 Jul 2007 17:39 GMT
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?

---------------------
Thomas 'PointedEars' Lahn - 29 Jul 2007 22:48 GMT
> 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:
>
[quoted text clipped - 4 lines]
> 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
David Mark - 29 Jul 2007 23:41 GMT
On Jul 29, 5:48 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
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
[quoted text clipped - 57 lines]
>       }
>      });

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.
Thomas 'PointedEars' Lahn - 30 Jul 2007 00:10 GMT
>>> Dot properties like .name1 are not replaced by anything new and they
>>> need to be "skipped" over by this regular expression but other name1
[quoted text clipped - 16 lines]
>
> 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
Mark Szlazak - 30 Jul 2007 18:33 GMT
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;
    }
}
Thomas 'PointedEars' Lahn - 30 Jul 2007 19:14 GMT
Please quote the minimum of what you are referring to:

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

>> Unfortunately, ECMAScript RegExp does not support negative lookbehind
>> (yet), so it would seem one has to live with the additional matches
[quoted text clipped - 6 lines]
>
> 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
Mark Szlazak - 31 Jul 2007 07:03 GMT
> > 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
[quoted text clipped - 13 lines]
>
> PointedEars

I'm not quite sure what you mean by this but if it's what I suspect
then this does not even work.
Thomas 'PointedEars' Lahn - 31 Jul 2007 10:28 GMT
>>> 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
[quoted text clipped - 12 lines]
>
> 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
Signature

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$8300dec7@news.demon.co.uk>

Mark Szlazak - 31 Jul 2007 16:46 GMT
On Jul 31, 2:28?am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
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
[quoted text clipped - 36 lines]
>
> - 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]
Randy Webb - 30 Jul 2007 01:01 GMT
Thomas 'PointedEars' Lahn said the following on 7/29/2007 5:48 PM:

<snip>

He's baaaaaaaack. School must be out.

Signature

Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.