Hi folks,
Consider the following JavaScript function:
function removeParam(str , name)
{
var rgx = new RegExp('(' + name + '=\\w*)|(' + name + '=\\w*;)');
rgx.global = true;
rgx.ignoreCase = true;
var matches = rgx.exec(str);
if(matches == null)
return str;
var i;
for(i = 0; i < matches.length; i++)
str = str.replace(mat ches[i], '');
return str;
}
and the following call:
var cookie = 'vid=39; vid=38; ASPNETSESSION=W HATEVER; vid=39';
alert(removePar am(cookie, 'vid'));
I actually would like to remove "vid=(whate ver number)" from the above
string, so that it results in the following string:
ASPNETSESSION=W HATEVER
would someone please helps me to fix the problem? I've got no idea
what's wrong with the above pattern I've written.
Thank you for your time.
Mehdi 3 1592
mehdi_mousavi wrote: Hi folks, Consider the following JavaScript function:
function removeParam(str , name) { var rgx = new RegExp('(' + name + '=\\w*)|(' + name + '=\\w*;)'); rgx.global = true; rgx.ignoreCase = true; var matches = rgx.exec(str); if(matches == null) return str;
var i; for(i = 0; i < matches.length; i++) str = str.replace(mat ches[i], ''); return str; }
and the following call:
var cookie = 'vid=39; vid=38; ASPNETSESSION=W HATEVER; vid=39'; alert(removePar am(cookie, 'vid'));
I actually would like to remove "vid=(whate ver number)" from the above string, so that it results in the following string:
ASPNETSESSION=W HATEVER
would someone please helps me to fix the problem? I've got no idea what's wrong with the above pattern I've written.
Thank you for your time.
For a global search .exec() does not return a straight array of
matches.
Use String.match() instead.
function removeParam(str , name)
{
var rgx = new RegExp(name + '=\\w*;*',"ig") ;
var matches = str.match(rgx);
if(matches != null)
for(var i = 0; i < matches.length; i++)
str = str.replace(mat ches[i], '');
return str;
}
// or
function removeParam(str , name)
{
return str.replace(new RegExp(name + '=\\w*;*',"ig") , '' );
}
--
S.C.
"mehdi_mous avi" <me***********@ gmail.com> writes: function removeParam(str , name) { var rgx = new RegExp('(' + name + '=\\w*)|(' + name + '=\\w*;)');
Could be shortened to
var rgx = new RegExp(name + "=\\w*;?","gi") ;
rgx.global = true; rgx.ignoreCase = true;
The "gi" above makes these two irrelevant too.
var matches = rgx.exec(str);
Here you should be aware of how the exec method works on regexps
with the global flag set.
It does not do all the possible matches in the string. Instead it
finds the first match and sets the property "index" on the regexp,
so the next call to exec starts at that index in the string (right
after the first match).
if(matches == null) return str;
var i; for(i = 0; i < matches.length; i++) str = str.replace(mat ches[i], ''); return str; }
Somewhat overkill:
return str.replace(rgx ,"");
and the following call:
var cookie = 'vid=39; vid=38; ASPNETSESSION=W HATEVER; vid=39'; alert(removePar am(cookie, 'vid'));
I actually would like to remove "vid=(whate ver number)" from the above string, so that it results in the following string:
ASPNETSESSION=W HATEVER
It will return
" ASPNETSESSION=W HATEVER; "
would someone please helps me to fix the problem? I've got no idea what's wrong with the above pattern I've written.
I would suggest another approach, where you parse the cookie instead
of treating it as a flat string:
---
function splitCookie(coo kie) {
var res = new Object();
var pairs = cookie.split(/;\s+/g);
for (var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split("=");
res[pair[0]] = pair[1];
}
return res;
}
var cookie = 'vid=39; vid=38; ASPNETSESSION=W HATEVER; vid=39';
var cookieData = splitCookie(coo kie);
var aspsession = cookieData["ASPNETSESS ION"];
alert(aspsessio n)
---
This is very simplified code. It does not make a special case for
keys that occour more than once, and it doesn not decode the cookie
keys or values, which might be encoded (using "escape" or url-encoding).
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.' This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Chamomile |
last post by:
I have to split strings of the type:
$str1 =' Large ladies hats 1.365 0.334';
$str2 = 'Pins 0.335 0.22';
into separate variables (or array members) :
say, $textStr, $number1, $number2 or $pieces $pieces $pieces
for inclusion in 3 separate fields of a database row
I am new to the world of regex and split() or preg_split() etc. and have
been trying all day to do this seemingly
|
by: Sugapablo |
last post by:
I admit, I'm terrible creating reg ex's.
I'm trying to create a preg_replace that would remove from a <a href> tag
that would replace the target attribute regardless of what the value might
be.
Any ideas?
--
|
by: Henry |
last post by:
I have a table that stores a list of zip codes using a varchar column type,
and I need to perform some string prefix pattern matching search. Let's say
that I have the columns:
94000-1235
94001
94100
If I run a pattern matching search for the string "940", then I should get
the top two rows of data back. If I run a pattern matching search for the
string "94", then I should get all the three rows of data back.
|
by: Matthet |
last post by:
Hello
I've got simpleType restricted by pattern
one element uses this simpleType
and I would like
another element to use this SimpleType
but with one extra pattern.
How to do it?
|
by: bissatch |
last post by:
Hi,
I am trying to use preg_replace to take out all occurances of PHP code
after reading (fread()) the contents of a PHP file. The code I am using
is:
$html = preg_replace("<?php*?>", "", $html);
Unfortuntely it is not working and giving the following error:
| |
by: SatishPasala |
last post by:
hi
I am tring to remove all the wild characters from a string. Is there
any short method to remove them in a single go.
I am right now tring to replace one by one.
Ex (999) 999-9999
I need 9999999999 with out all the charecters and spaces. I am trying
|
by: tom fredriksen |
last post by:
Hi
I was doing a simple test of the speed of a "maths" operation and when I
tested it I found that removing the loop that initialises the data array
for the operation caused the whole program to spend twice the time to
complete. If the loop is included it takes about 7.48 seconds to
complete, but when removed it takes about 11.48 seconds.
Does anybody have a suggestion as to why this is so and whether I can
trust the results of the...
|
by: Eric_Dexter |
last post by:
def simplecsdtoorc(filename):
file = open(filename,"r")
alllines = file.read_until("</CsInstruments>")
pattern1 = re.compile("</")
orcfilename = filename + "orc"
for line in alllines:
if not pattern1
print >>orcfilename, line
I am pretty sure my code isn't close to what I want. I need to be able
|
by: Schroeder, AJ |
last post by:
Hello group,
I am attempting to remove double quotes from the beginning and ending of a
string. Admittedly, I am not the best with regular expressions, but I do
have two that work with preg_replace(). Here they are:
$pattern = ''; <--- removes the beginning double quote
$pattern = ''; <--- removes the ending double quote
But when I try to make this all one statement with:
|
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...
|
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...
| |
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |