Tim Webster
Tim Webster

Reputation: 9196

What does this function do? And what is the odd syntax?

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?

Upvotes: 0

Views: 100

Answers (3)

keegan3d
keegan3d

Reputation: 11275

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.

Upvotes: 3

Sandeep Rajoria
Sandeep Rajoria

Reputation: 1239

/from_url=$/

is a regular expression which should translate to check the temp and find if it has 'from_url=' this text at the end of the string

Upvotes: 1

Tadeck
Tadeck

Reputation: 137300

/from_url=$/

is a regular expression literal in JavaScript. You can replace it with

new RegExp('from_url=$')

This specific regular expression checks if the string ends with "from_url=" string.

Upvotes: 0

Related Questions