/    Sign up×
Community /Pin to ProfileBookmark

JS noob… Question with Regex

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 [URL=”http://www.elated.com/articles/javascript-and-cookies/”]THIS[/URL] 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;
}
[/CODE]

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 ?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangOct 11.2010 — 
  • 1. Single or double quote,: it doesn't really mater, just the writers preference.


  • 2. Vertical bar is the OR operator. In the first case the match is for a circumflex or semi-colon.


  • http://www.javascriptkit.com/jsref/regexp.shtml
    Copy linkTweet thisAlerts:
    @kaworitoauthorOct 11.2010 — Thanks for your fast answer!!

    But I think in the first case the circumflex is the indicator to match the beggining of a string... from the link you posted "^ Only matches the beginning of a string: /^The/ matches "The" in "The night" by not "In The Night""

    If it was supposed to be the '^' character, they would've written ^ to escape the special character.

    So it's an OR operation between the semicolon and nothing else, and that's where my confusion is...

    thanks for your help again ?
    Copy linkTweet thisAlerts:
    @FangOct 11.2010 — The circumflex has a variety of meanings depending on it's use:

    /^ foo/ : matches the beginning of a string

    /^ foo/ : character in pattern

    [^foo] : negated pattern matching

    (^|? : character in pattern

    Yes, the vertical bar is just an OR operator
    Copy linkTweet thisAlerts:
    @kaworitoauthorOct 11.2010 — Awesome! didn't know that! ?

    Thanks for your help!
    ×

    Success!

    Help @kaworito spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...