Take the 2-minute tour ×

In the jquery below should there be a closing paretheses ")" before the "dot" in:

".test($ ..."?


$(document).ready(function () {
    $('#userlogin').css('color', '#cccccc').val('LOGINNAME');   

    $('#userlogin').blur(function () {
        if (/^\s*$/.test($(this).val())) {
            $(this).css('color', '#cccccc');
    }).focus(function () {
        if ($(this).val() === 'LOGINNAME') {
            $(this).css('color', '#000000');

If not, why not? That code looks a little weird to me.

share|improve this question
Well, does it work? –  Juhana May 18 '12 at 16:01
/something/ is a regular expression. In Javascript they are objects with methods, and test is one such method. There's nothing wrong with the code. –  David Ellis May 18 '12 at 16:03
There is one more way to avoid confusion like this and that is proper code formatting with some whitespace to render the code les cryptic –  nus May 18 '12 at 22:04
add comment

4 Answers

up vote 4 down vote accepted

The code is correct:

/^\s*$/  // create a regex
  .test( // call the test method on it
    $(this) // create a jquery object
      .val() // call the val method on it

However, the value could be cached: var val = $(this).val();. Then the line might be less confusing to you: /yourregex/.test(val)

share|improve this answer
what does that regular expression mean? no blank spaces? Not sure... –  Chris22 May 18 '12 at 18:48
It matches a string that is empty or consists of nothing but whitespace. –  ThiefMaster May 18 '12 at 19:34
add comment

/^\s*$/ is a regular expression literal and it has a test method which is being called. The brackets are from the if condition which is bigger.

Since javascript is fully object oriented even literals can have methods.

share|improve this answer
+1. if I could select more than one answer, I would. Thanks for explaining that regular expressions are literals and have methods. That really helped. –  Chris22 May 18 '12 at 18:49
add comment

The code is fine. If you think its unreadable why not create the regex first

var regex = /^\s*$/;
share|improve this answer
I'm pretty sure you pass a string when using new RegExp(). However, don't do this unless you have to. Using a string means you have to double-escape backslashes etc. –  ThiefMaster May 18 '12 at 16:04
Oh right. It's a while since I've done regexs in JS. Thanks for the input –  Ash Burlaczenko May 18 '12 at 16:06
@Ash, yes if it were coded like that, I would have understood it a ilttle better. I didn't know reg expressions could have methods –  Chris22 May 18 '12 at 18:51
@AshBurlaczenko, +1 –  Chris22 May 18 '12 at 18:57
add comment

The first part is a regular expression - not a string or anything else. As such, it begins and ends with deliminters / and /.

Using .match is sometimes more legible as it doesn't appear to have some strange unbound item floating around in the source:


This particular expression, broken down, reads like this:

/^   // Beginning of String
\s*  // Space, zero or more times
$/   // End of String

So it will test positive if there are zero or more spaces, from start to finish, and nothing else.

share|improve this answer
There actually shouldn't be a paren at the beginning - he copied from his if() statement –  ThiefMaster May 18 '12 at 16:04
@ThiefMaster Ah, good eye. –  Jonathan Sampson May 18 '12 at 16:04
@JonathanSampson, +1 this helps me as well to understand. I found this link on google developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions –  Chris22 May 18 '12 at 18:56
@Chris22 The Mozilla Developer network is always helpful. I also noted that you had asked what this expression meant, so I've updated my answer to contain that explanation as well. –  Jonathan Sampson May 18 '12 at 18:59
add comment

Your Answer


