Search code examples
javascriptregexsyntax

What does that syntax mean where some characters are surrounded forward slashes (`/ … /`)?


Can someone explain what this piece of code does. what is the test function testing for?

temp = "blah"
if ( /from_url=$/.test(temp) ) {
    //do something
}

test : function(s, p) {
    s = s.nodeType == 1 ? s.value : s;

    return s == '' || new RegExp(p).test(s);
}

Also in the initial condition what does the syntax if(/from_url=$/) do?


Solution

  • s = s.nodeType == 1 ? s.value : s; if s.nodeType is 1 then use s.value, otherwise use s.

    return s == '' || new RegExp(p).test(s); return s if it's an empty string, otherwise test if s is in the regular expression p.

    if(/from_url=$/) is a regular expression that is looking for from_url= but only if it is at the very end.