0

I have found the below Javascript recently, and (believe) I understand its operation, but cannot figure out (what appears) to be a ¿regex string class? ("/\W/.test")

   function AlphaNumericStringCheck(text) 
   {
       if (/\W/.test(text.replace(/^\s+|\s+$/g,""))) return false;
       return true;
   }

Can someone put a name to this technique, so I can research it more?

9
  • /\W/ is a regular expression literal. Regular expressions have methods you can invoke. Have a look at developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions and developer.mozilla.org/en/JavaScript/Reference/Global_Objects/… Commented Mar 1, 2012 at 18:19
  • It's not clear to me what your question is--are you asking about the immediate regex expressions? What do you mean by "regex string class"? It's a regex, not a string. Commented Mar 1, 2012 at 18:20
  • Are you referring to the method test() on the content between slashes? If so that would be the RegExp object in JavaScript. It is merely a consolidated syntax for regular expressions in JS. w3schools.com/jsref/jsref_obj_regexp.asp Commented Mar 1, 2012 at 18:21
  • @Felix Kling - Thanks. I was aware of the two ways of invoking a regex object in that article, but have never seen this way of invoking a Regular Expression Commented Mar 1, 2012 at 18:36
  • @Dave Newton - Sorry, it was my way of describing what looked to me to be a /\W/ object (my mistake on classname) and test function being called, but thought you couldn't start a object name with anything but a alphanumeric character Commented Mar 1, 2012 at 18:40

2 Answers 2

4

The /\W/ in your source code is a regular expression literal (MDC link, as MDC is about 18X clearer than the specification). Just as with a string literal ("foo"), a regular expression literal is a way of writing regular expressions in the code. The / characters in a regular expression literal are analogous to the quote characters in a string literal. In a string literal, what's inside the quotes is the content of the string; in a regular expression literal, what's inside the / characters is the regular expression. (There can also be flags following the ending /.)

So this:

var rex = /\W/;

...creates a regular expression object for the regular expression \W (match one word character). It's (essentially) equivalent to:

var rex = new RegExp("\\W");

Note that in the long form, I had to escape the backslash in the string, since backslashes are special in string literals. This is one of the reasons we have regular expression literals: Because it gets very confusing, very quickly, when you have to escape all of your backslashes (backslashes being a significant part of many regular expressions).

Regular expressions are objects, which have properties with functions attached to them (effectively, methods, although JavaScript doesn't technically have methods per se). So /\W/.test(...) calls the test function on the regular expression object defined by the literal /\W/.

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

3 Comments

Thanks T.H Crowder. "So /\W/.test(...) calls the test function on the regular expression object defined by the literal /\W/" is the sort of answer I was looking for. have not seen this form before, and obviously don't have a full understanding of what can be done with a functions & objects in JS. Side note - Would be good if the MDC document would have listed this form as an example!
@user66001: Glad that helped! Re the MDC doc: It has several examples of literals, and even has this example buried in it: "/\d+(?!\.)/.exec("3.141") matches 141 but not 3.141."
So it does, though I didn't read about regex, as I already am comfortable with that :(
0

\W is a shortcut (shorthand character classes or extensions) for word. Just like \d for digits and \s for whitespace and new lines. It depends on the implementation of the regex you're using.

This literals are replaced by full expression before compiled. \d turns into [0-9] and \W probably turns into [0-9a-Z][0-9a-Z]* or similar. They are designed to make your expressions more readable.

You can see some more of them here: http://www.zytrax.com/tech/web/regex.htm#special

1 Comment

Thanks Guax. Should have probably mentioned I am very familiar with RegEx, and the example regex was not the issue, just the /\W/ object.

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.