2

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

".test($ ..."?

(/^\s*$/.test($(this).val()))

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

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

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

2
  • 1
    /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. Commented May 18, 2012 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 Commented May 18, 2012 at 22:04

4 Answers 4

4

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)

Sign up to request clarification or add additional context in comments.

1 Comment

It matches a string that is empty or consists of nothing but whitespace.
2

/^\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.

1 Comment

+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.
2

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

var regex = /^\s*$/;
if(regex.test($(this).val())
    ...

3 Comments

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.
Oh right. It's a while since I've done regexs in JS. Thanks for the input
@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
2
/^\s*$/.test( $(this).val() )

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).val().match(/^\s*$/);

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.

3 Comments

There actually shouldn't be a paren at the beginning - he copied from his if() statement
@JonathanSampson, +1 this helps me as well to understand. I found this link on google developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.