473,725 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.rep lace("^" + iName + "=,($|&)", "");
---------------------------------

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

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

But I need to use the "iName" variable instead of "default.as px".
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 3406
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.rep lace("^" + iName + "=,($|&)", "");
---------------------------------

The following DOES replace it though:
---------------------------------
var match = myStringVar.mat ch("^" + iName + "=,($|&)");
if(match != null && match.length 0) myStringVar =
myStringVar.rep lace(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.rep lace(/^default.aspx=, ($|&)/, "");
---------------------------------
The reason for that is that String.prototyp e.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.prototyp e.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.prototyp e.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.prototyp e.replace (searchValue, replaceValue)
|
| [...]
| If `searchValue' is a regular expression (an object whose [[Class]]
| property is "RegExp"), do the following: If searchValue.glo bal is
| false, then search `string' for the first match of the regular
| expression `searchValue'. If `searchValue.gl obal' is true, then
| search string for all matches of the regular expression
| `searchValue'. [...]
|
| If searchValue is not a regular expression, let `searchString' be
| `ToString(searc hValue)' and search `string' for the first
| occurrence of `searchString'. [...]

So the string argument in your String.prototyp e.replace() call denotes a
*literal* string, including the literal `($|&)', which does not match.
But I need to use the "iName" variable instead of "default.as px".
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.rep lace(
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
4177
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 regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
5
2691
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 function to verify the name fields, age, email and gender. My question is: if I create a function for each field like the code below, what would be the best way to organize the functions and call them? Would I need one main function and place...
9
2727
by: Lyners | last post by:
Quick question. I have some java script that looks like this; imgObj.parentNode.parentNode.childNodes(10).childNodes(0).value=imgObj.parentNode.parentNode.childNodes(6).innerText-imgObj.parentNode.parentNode.childNodes(9).childNodes(0).value This takes a cell in a datagrid (innertext) and subtracts the value of a text box (value) in another cell, then populates a second text box in a third cell with the value. it works except for the...
7
3828
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 want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
0
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9174
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9111
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8096
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4517
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3221
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
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.