473,574 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with substring match

Rob
I've just about beat my brains all the way out on this one. Is anyone
up to the amazingly ridiculous challenge of figuring this one out?
Here's the code, followed by my question:

<script type="text/javascript">
function readCookie(valu ename)
{
var the_cookie = document.cookie ;
the_cookie = unescape(the_co okie);
var nameAndValue = new RegExp( valuename + "=[a-z0-9]*" );
the_cookie = the_cookie.matc h( nameAndValue );

// so far so good, I now have "name=value " stored in the_cookie

var onlyEqualsAndVa lue = new RegExp( "=[a-z]*" );
the_cookie = the_cookie.matc h( onlyEqualsAndVa lue );
alert(the_cooki e);
}
</script>

Why doesn't the second call to match() give me just "=value"? The
first regex works flawlessly, but the second one fails for no reason
that I can see--and in fact the alert only pops up if I don't attempt
to make the second call to match(). WTF?

TIA,
Rob

Mar 9 '07 #1
2 1789
The match() method returns an array, rather than a string. The
matching string is contained in the 0 index of the array. So the line:

the_cookie = the_cookie.matc h( nameAndValue );

Returns an array into "the_cookie ". However, when you later execute:

the_cookie = the_cookie.matc h( onlyEqualsAndVa lue );

You are treating "the_cookie " as a string. This line should actually
read:

the_cookie = the_cookie[0].match( onlyEqualsAndVa lue );

That is, you call the match() method on the string contained within
the first index of the "the_cookie " array.

However, you could simply use the following code to get your value:

var nameAndValue = new RegExp( valuename + "=([a-z0-9]*)" );
var matches = the_cookie.matc h( nameAndValue );
alert(matches[1]);

matches[0] holds the entire matching string, matches[1] holds the
first parenthesised substring in the matching string.

wp.

Mar 9 '07 #2
Rob
On Mar 9, 6:55 am, "wisestpota to" <wisestpot...@g ooglemail.comwr ote:
The match() method returns an array, rather than a string. The
matching string is contained in the 0 index of the array. So the line:

the_cookie = the_cookie.matc h( nameAndValue );

Returns an array into "the_cookie ". However, when you later execute:

the_cookie = the_cookie.matc h( onlyEqualsAndVa lue );

You are treating "the_cookie " as a string. This line should actually
read:

the_cookie = the_cookie[0].match( onlyEqualsAndVa lue );

That is, you call the match() method on the string contained within
the first index of the "the_cookie " array.

However, you could simply use the following code to get your value:

var nameAndValue = new RegExp( valuename + "=([a-z0-9]*)" );
var matches = the_cookie.matc h( nameAndValue );
alert(matches[1]);

matches[0] holds the entire matching string, matches[1] holds the
first parenthesised substring in the matching string.

wp.

Wow, you're EXACTLY right--thank you so much! I'm ashamed to admit
that if I had RTFM ("JavaScript The Definitive Guide"), I could have
answered my own question.

Thanks again!
Rob

Mar 9 '07 #3

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

Similar topics

7
8571
by: Radhika Sambamurti | last post by:
Hi, I've written a substring function. The prototype is: int substr(char s1, char s2) Returns 1 if s2 is a substring of s1, else it returns 0. I have written this program, but Im sure there is an easier way to do this. I am hoping someone can point me to a more elegant way of writing this same function. //// code follows /////////// ...
11
1904
by: Rolf Barbakken | last post by:
I have an xml with records like this one: <a:response> <a:href>http://server/public/sol/comp/1049306.eml</a:href> <a:propstat> <a:status>HTTP/1.1 200 OK</a:status> <a:prop> <d:customerid>1049306</d:customerid> <e:professioncode b:dt="mv.string"> <c:v>byggtapetserarbeider</c:v>
3
10302
by: Tim Soliday | last post by:
I have been looking at regular expressions lately, and I'm hoping that maybe someone could help me? I am trying to find the position of a substring, and then returning the substring including any information that I might want whether it is to the left or right of that substring. For example: //Position of substring has already been...
0
3331
by: Ewart MacLucas | last post by:
generated some WMI managed classes using the downloadable extensions for vs2003 from mircrosoft downloads; wrote some test code to enumerate the physicall processors and it works a treat, but a question.. The code fails with the error that: "Additional information: COM object that has been separated from its underlying RCW can not be used."...
5
10174
by: JackRazz | last post by:
Anyone know the regular expression to match a blank line where the byte sequence is "0D 0A 0D 0A" ive tried "\r\n\r\n+", "^$+" "\n\r" with no success. Any Ideas? Thanks - JackRazz This is the code fragment I'm trying Dim r As Regex, m As Match, i As Integer
1
1708
by: sommarlov | last post by:
Hi everyone >From one of our systems an xml file is produced. I need to validate this file before we send it to an external system for a very lenghty process. I cannot change the xml file layout. The solution i got today is very slow, and i need help to find another solution. Here is the xml file. It consists of a list of position ids...
15
2084
by: lucky | last post by:
Hi Guys You are probably my last chance to avoid getting crazy To help you understand my problem I have put images online showing the issue I have: http://www.australix.net/images/pb I have an XML file containing links and I use an XSL file to build a
15
5985
by: nagar | last post by:
I need to split a string whenever a separator string is present (lets sey #Key(val) where val is a variable) and rejoin it in the proper order after doing some processing. Is there a way to use the Regex.Split function to split the string whenever the #Key(val) occurrs but that keeps the #Key(val) occurrences to that I can reconstruct the...
4
1940
by: Flomo Togba Kwele | last post by:
I am having difficulty writing a Regex constructor. A line has a quote(") at its beginning and its end. I need to strip both characters off. If the line looks like "1", I need the result to be 1. If it were sed, I could just do s/^"// and s/"$//, but I'm confused with the quote escape characters. Also, can I do both replacements at once? ...
0
7832
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...
0
7747
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8093
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. ...
0
8262
jinu1996
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...
1
5643
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...
0
5331
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3768
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...
0
3785
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2262
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 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.