HighDots Forums  

Javascript Regular Expression with Replace

Javascript JavaScript language (comp.lang.javascript)


Discuss Javascript Regular Expression with Replace in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
NvrBst
 
Posts: n/a

Default Javascript Regular Expression with Replace - 12-14-2007 , 03:17 PM






I want to use the .replace() method with the regular expression /^ %VAR
% =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)"
regular expression with "":
---------------------------------
myStringVar = myStringVar.replace("^" + iName + "=,($|&)", "");
---------------------------------

The following DOES replace it though:
---------------------------------
var match = myStringVar.match("^" + iName + "=,($|&)");
if(match != null && match.length > 0) myStringVar =
myStringVar.replace(match[0], "");
---------------------------------

Am I using the replace method wrong? The following DOES work when
using the replace method for me:
---------------------------------
myStringVar = myStringVar.replace(/^default.aspx=,($|&)/, "");
---------------------------------

But I need to use the "iName" variable instead of "default.aspx".
Does replace not let me use strings as regular expressions
like .match() and .search()? Anyone know how I would get .replace()
to work the way I want it to? Reasion I'm curious is because the 2nd
method above takes about 2 seconds to complete on my machine when I
have a feeling the single ".replace()" would probably take 1/2 the
time.

Thanks

NB

Reply With Quote
  #2  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Javascript Regular Expression with Replace - 12-14-2007 , 04:48 PM






NvrBst wrote:
Quote:
I want to use the .replace() method with the regular expression /^ %VAR
% =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)"
regular expression with "":
---------------------------------
myStringVar = myStringVar.replace("^" + iName + "=,($|&)", "");
---------------------------------

The following DOES replace it though:
---------------------------------
var match = myStringVar.match("^" + iName + "=,($|&)");
if(match != null && match.length > 0) myStringVar =
myStringVar.replace(match[0], "");
---------------------------------

Am I using the replace method wrong?
Yes, you do.

Quote:
The following DOES work when using the replace method for me:
---------------------------------
myStringVar = myStringVar.replace(/^default.aspx=,($|&)/, "");
---------------------------------
The reason for that is that String.prototype.replace() takes both a RegExp
object reference and a string value as first argument. However, when passed
a string value, it works differently. While String.prototype.match() always
expects a RegExp object reference and converts the argument into one if it
is not one already.

From the ECMAScript Specification, Edition 3 Final:

Quote:
15.5.4.10 String.prototype.match (regexp)

If `regexp' is not an object whose [[Class]] property is "RegExp",
it is replaced with the result of the expression `new RegExp(regexp)'.
[...]

15.5.4.11 String.prototype.replace (searchValue, replaceValue)

[...]
If `searchValue' is a regular expression (an object whose [[Class]]
property is "RegExp"), do the following: If searchValue.global is
false, then search `string' for the first match of the regular
expression `searchValue'. If `searchValue.global' is true, then
search string for all matches of the regular expression
`searchValue'. [...]

If searchValue is not a regular expression, let `searchString' be
`ToString(searchValue)' and search `string' for the first
occurrence of `searchString'. [...]
So the string argument in your String.prototype.replace() call denotes a
*literal* string, including the literal `($|&)', which does not match.

Quote:
But I need to use the "iName" variable instead of "default.aspx".
Does replace not let me use strings as regular expressions
like .match() and .search()?
A string value is not a RegExp object reference, so you can not use it as
one. However, if the implementation does not already do it for you (as in
this case), you can convert it into a Regular Expression manually, provided
you escape potential special characters if they are meant to be literally in
the match.

Quote:
Anyone know how I would get .replace() to work the way I want it to?
myStringVar = myStringVar.replace(
new RegExp("^" + iName + "=,($|&)"),
"");

I don't know what you actually want to match, but you should consider
escaping the `$' with `\\$' if it is not meant as end-of-input.

Quote:
Reasion I'm curious is because the 2nd method above takes about 2 seconds
to complete on my machine when I have a feeling the single ".replace()"
would probably take 1/2 the time.
ISTM you should consider other factors as reasons for this.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.