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

Removing a simple pattern from a string!

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(matches[i], '');
return str;
}

and the following call:

var cookie = 'vid=39; vid=38; ASPNETSESSION=WHATEVER; vid=39';
alert(removeParam(cookie, 'vid'));

I actually would like to remove "vid=(whatever number)" from the above
string, so that it results in the following string:

ASPNETSESSION=WHATEVER

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

Apr 18 '06 #1
3 1573

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(matches[i], '');
return str;
}

and the following call:

var cookie = 'vid=39; vid=38; ASPNETSESSION=WHATEVER; vid=39';
alert(removeParam(cookie, 'vid'));

I actually would like to remove "vid=(whatever number)" from the above
string, so that it results in the following string:

ASPNETSESSION=WHATEVER

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(matches[i], '');

return str;
}

// or

function removeParam(str, name)
{
return str.replace(new RegExp(name + '=\\w*;*',"ig"), '' );
}

--
S.C.

Apr 18 '06 #2

Stephen Chalmers wrote:
For a global search .exec() does not return a straight array of
matches.
Actually that's not quite what I meant to say, you can see the
structure of the data returned here:

http://developer.mozilla.org/en/docs...ts:RegExp:exec
S.C.


Apr 18 '06 #3
"mehdi_mousavi" <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(matches[i], '');
return str;
}
Somewhat overkill:
return str.replace(rgx,"");
and the following call:

var cookie = 'vid=39; vid=38; ASPNETSESSION=WHATEVER; vid=39';
alert(removeParam(cookie, 'vid'));

I actually would like to remove "vid=(whatever number)" from the above
string, so that it results in the following string:

ASPNETSESSION=WHATEVER
It will return
" ASPNETSESSION=WHATEVER; "
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(cookie) {
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=WHATEVER; vid=39';
var cookieData = splitCookie(cookie);
var aspsession = cookieData["ASPNETSESSION"];
alert(aspsession)
---

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/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Apr 18 '06 #4

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

Similar topics

5
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,...
9
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....
1
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...
3
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?
16
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*?>", "",...
4
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...
102
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...
17
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...
6
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.