24

The regex allows chars that are: alphanumeric, space, '-', '_', '&', '()' and '/'

this is the expression

[\s\/\)\(\w&-]

I have tested this in various online testers and know it works, I just can't get it to work correctly in code. I get sysntax errors with anything I try.. any suggestions?

var programProductRegex = new RegExp([\s\/\)\(\w&-]);
3
  • No need to escape parentheses in a character class. i.e. use: /[-\s\/()\w&]/ and remember that a dash must be escaped except when at the beginning or end of the class. See: regular-expressions.info for a great tutorial. Commented Apr 1, 2011 at 16:08
  • I noticed in the jQuery.uni-form.js file that forward slashes inside a character class are not being escaped, and yet the code works fine. Is it possible that escaping forward slashes in a character class is optional? I'd like to know for the benefit of the TextMate Javascript bundle, used also by Sublime Text 2. Commented Apr 17, 2012 at 5:17
  • @ScottLahteine I think (never found any documentation on this) that only in RegEx literals /slashes\/must/ be escaped. When using the RegExp constructor it does not have be be escapted Commented Jan 7, 2013 at 14:35

3 Answers 3

37

You can use the regular expression syntax:

var programProductRegex = /[\s\/\)\(\w&-]/;

You use forward slashes to delimit the regex pattern.

If you use the RegExp object constructor you need to pass in a string. Because backslashes are special escape characters inside JavaScript strings and they're also escape characters in regular expressions, you need to use two backslashes to do a regex escape inside a string. The equivalent code using a string would then be:

var programProductRegex  = new RegExp("[\\s\\/\\)\\(\\w&-]");

All the backslashes that were in the original regular expression need to be escaped in the string to be correctly interpreted as backslashes.

Of course the first option is better. The constructor is helpful when you obtain a string from somewhere and want to make a regular expression out of it.

var programProductRegex  = new RegExp(userInput);
Sign up to request clarification or add additional context in comments.

5 Comments

why do you have to put one at the end, is that basically a delimeter?
Note also that you do not need to escape() parentheses in a character class.
@ridgerunner: Yes, and neither the forward slashes, but I kept them to maintain consistency with the original pattern.
Nice tip with the double slashes. (boo for Javascript for being as annoying as Java in this regard)
Thank you! I have been looking for this explanation for a while!
7

If you are using a String and want to escape characters like (, you need to write \\( (meaning writing backslash, then the opening parenthesis => escaping it).

If you are using the RegExp object, you only need one backslash for each character (like \()

Comments

1

Enclose your regex with delimiters:

var programProductRegex = /[\s\/)(\w&-]/;

1 Comment

Probably because you're passing a RegExp literal into a RegExp constructor. If you have a RegExp literal, using the RegExp constructor isn't necessary.

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.