0

I navigated to a file while browsing the source code of a project. It is a javascript file and it contains the following:

var Tools = {};
... 
Tools.EMAIL_ADDR_PATTERN = /\w+@[a-zA-Z0-9_-]+?(?:\.[a-zA-Z]{2,6})+/gim;

What type is the EMAIL_ADDR_PATTERN? Why is capitalised? How can be invoked?

This might be quite simple, however my js knowledge is very limited.

5
  • 1
    It's an email address pattern. What do you want from this? Commented Jul 14, 2014 at 13:01
  • why don't you just google regular expressions? MDN and a small toy. Commented Jul 14, 2014 at 13:01
  • [Any alpha numeric char with no length limit]@[Any alpha numeric char, _ and - with no length limit].[Any char to a to z, A to Z with limited length between 2 and 6] in short. Reference Commented Jul 14, 2014 at 13:02
  • 1
    should be noted that the a-z A-Z is redundant, as it's matched case-insensitive (/..../i) anyway. Commented Jul 14, 2014 at 13:06
  • @amenthes quite ironic that the author used \w in the beginning but then went for [a-zA-Z0-9_-] instead of [\w-]. Commented Jul 14, 2014 at 13:08

5 Answers 5

3

This is literally what it looks like. A regular expression literal is assigned to the variable Tools.EMAIL_ADDR_PATTERN. There's nothing special about capitalisation, it's likely a convention in the code to emulate constants.

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

Comments

1

What type is the EMAIL_ADDR_PATTERN? Why is capitalised?

EMAIL_ADDR_PATTERN is the name of the property that the programmer has chosen. It can be anything.

How can be invoked?

Use the pattern in any method that accepts Regular Expression pattern such as:string.match(Tools.EMAIL_ADDR_PATTERN)

Comments

1

Short answer: it's regular expression literal.

You can open a console in your favorite browser (press F12) and try for yourself:

> var r = /\w+@[a-zA-Z0-9_-]+?(?:\.[a-zA-Z]{2,6})+/gim
> typeof(r)
"object"
> r.constructor
function RegExp() { [native code] }
> r.test("hello world")
false
> r.test("[email protected]")
true

A REPL console is always a good friend. The Chrome one will even auto-complete method names so you can discover them more easily.

Comments

0

This is how you would use it:

var email = "[email protected]";

if (email.match(Tools.EMAIL_ADDR_PATTERN)) {
  alert("this is an email");
}

Comments

0

Tools.EMAIL_ADDR_PATTERN just to validate the email string.

you can use like:

var flag = /\w+@[a-zA-Z0-9_-]+?(?:\.[a-zA-Z]{2,6})+/gim.test('[email protected]')

flag will be (boolean)true

Comments

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.