0

The box should allow:

  • Uppercase and lowercase letters (case insensitive)
  • The digits 0 through 9
  • The characters, ! # $ % & ' * + - / = ? ^ _ ` { | } ~
  • The character "." provided that it is not the first or last character
3
  • Have you tried something already? If so, could you post is as well? Commented Jan 31, 2011 at 7:45
  • var ex = new RegExp("[a-z,0-9,!,#,$,%,&,',*,+,-,/,=,?,^,_,,{,|,},~][a-z,0-9,!,#,$,%,&,',*,+,-,/,=,?,^,_,,{,|,},~,.]+[a-z,0-9,!,#,$,%,&,',*,+,-,/,=,?,^,_,`,{,|,},~]$"); Commented Jan 31, 2011 at 7:49
  • I'm not getting how to create a regex where first character matches [a-z] AND [0-9] and [%$#] etc. Do we use a comma for those? Commented Jan 31, 2011 at 7:51

4 Answers 4

3

Try

^(?!\.)(?!.*\.$)[\w.!#$%&'*+\/=?^`{|}~-]*$

Explanation:

^         # Anchor the match at the start of the string
(?!\.)    # Assert that the first characters isn't a dot
(?!.*\.$) # Assert that the last characters isn't a dot
[\w.!#$%&'*+\/=?^`{|}~-]*   # Match any number of allowed characters
$         # Anchor the match at the end of the string
Sign up to request clarification or add additional context in comments.

Comments

1

Try something like this:

// the '.' is not included in this:
var temp = "\\w,!#$%&'*+/=?^`{|}~-";

var regex = new RegExp("^["+ temp + "]([." + temp + "]*[" + temp + "])?$");
//                                      ^
//                                      |
//                                      +---- the '.' included here

Looking at your comments it's clear you don't know exactly what a character class does. You don't need to separate the characters with comma's. The character class:

[0-9,a-z]

matches a single (ascii) -digit or lower case letter OR a comma. Note that \w is a "short hand class" that equals [a-zA-Z0-9_]

More information on character classes can be found here:

http://www.regular-expressions.info/charclass.html

2 Comments

I like the temp trick to avoid some duplication, but it hides the fact - isn't the last character in your class anymore: you end up with [~-.] (I'm not sure what it does, though).
I tried and I ended up with "invalid character range" because the last character in the temp variable in temp is a dash, it should be quoted. Afterward line boundaries should be added to ensure that the first and the last "groups" are really the first and the last character. This is my edit: var temp = "\\w,!#$%&'*+/=?^`{|}~\\-"; var regex = new RegExp("^["+ temp + "][" + temp + ".]*[" + temp + "]$"); But this works only with at least two characters in the string. For example it won't match "a"
0

You can do something like:

^[a-zA-Z0-9,!#$%&'*+-/=?^_`{|}~][a-zA-Z0-9,!#$%&'*+-/=?^_`{|}~.]*[a-zA-Z0-9,!#$%&'*+-/=?^_`{|}~]$

Comments

0

Here's how I would do it:

/^[\w!#$%&'*+\/=?^`{|}~-]+(?:\.[\w!#$%&'*+\/=?^`{|}~-]+)*$/

The first part is required to match at least one non-dot character, but everything else is optional, allowing it to match a string with only one (non-dot) character. Whenever a dot is encountered, at least one non-dot character must follow, so it won't match a string that begins or ends with a dot.

It also won't match a string with two or more consecutive dots in it. You didn't specify that, but it's usually one of the requirements when people ask for patterns like this. If you want to permit consecutive dots, just change the \. to \.+.

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.