0

I am new to regular expressions and am having trouble setting one up. What I want is to allow only alphabets, numbers, commas, periods and hyphens. This is what I got:

var letters = /^[a-zA-Z0-9,. ]*$/;

I am having trouble figuring out how to include the hyphen. Please assist.

2
  • 2
    var letters = /^[-a-zA-Z0-9,. ]+$/; Commented Oct 24, 2015 at 12:07
  • By hyphens, do you mean only the regular dash (-), or other hyphens? Commented Oct 24, 2015 at 12:10

2 Answers 2

1

You can include the minus where it won't be interpreted as a range:

var letters = /^[-a-zA-Z0-9,. ]*$/;

You can also use backslash to specify that it's a literal character:

var letters = /^[a-zA-Z0-9,\-. ]*$/;
Sign up to request clarification or add additional context in comments.

Comments

0
var letters = /^[a-zA-Z0-9\-\,. ]+$/;

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.