JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 11th, 2010, 04:38 AM
kaworito kaworito is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2010
Posts: 1 kaworito User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m
Reputation Power: 0
New with Javascript... please help with regex patterns :)

Hey all!

I'm pretty new to JS and I've been reading online tutorials to get the hang of it. I usually don't go forward on the tutorials until I understand everything of what I'm learning and right now I'm stuck at trying to understand and figure out how Regex patterns work...

So, I was reading THIS cookies tutorial and I got stuck at one point...

On the function to retrieve the cookies from the visitor's computer, they use a matching function that tries to get the value of the cookie...
Code:
function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}


I have a few questions.
1. Why is the Regular Expression in a single quote instead of the '/'?
2. about the Regex patterns... I don't understand why they use the vertical bar in both groups (^|? and (;|$)... after a lot of thought and research I kinda figured out that they are trying to search for a semi colon that marks the beginning of the name of each cookie stored on the document.cookie but if they don't find it it's alright since it means it's the beginning of the document. And the same at the end of the line/document. But still I don't understand why they use the vertical bar if they're not trying to find two values...
I used a Regex tester online and tried to erase the vertical bars, but it stopped matching the cookie string. So I'm just really confused. I know what the pattern does but I don't understand exactly how it's working.

Hope I was clear enough! English is not my mother language so I apologize if this was unclear or hard to read. But please help me

Reply With Quote
  #2  
Old October 11th, 2010, 10:14 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
Welcome to DevShed Forums, kaworito.

1) String.match() can accept a string instead of a RegExp as the argument. The string will then be automatically converted to a RegExp. In this case it allows the variable "cookie_name" to be concatenated with other strings to form the string for the expression to match against.

2) In regular expressions the "|" means "or", so those two sub groups either match one end of the string or a ";" character.
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

Remember people spend most of their time on other people's sites (so don't violate web design conventions).

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > New with Javascript... please help with regex patterns :)

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap