473,230 Members | 1,425 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,230 software developers and data experts.

Javascript Regular Expression with Replace

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
Dec 14 '07 #1
1 3348
NvrBst wrote:
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.
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:

| 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.
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.
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.
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
Dec 14 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
9
by: Lyners | last post by:
Quick question. I have some java script that looks like this; ...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.