0

I have to put a given variable into a regular expression. When I do it with hard coded data it works. Here is my code for that

/^(?=.*[a-z])+(?=.*[A-Z])+(?=.*[0-9@#$-/:-?{-~!"^_`\[\]])+((?!ccarn).)*$/

This should ( and does ) look for a word (password in this case) that is case sensitive, has at least one capitol and one lowercase letter, and one number or symbol. It cannot, however, contain the word "ccarn" in it. Again when I put this in as my regex all works out. When I try to turn it into a string that gets passed in, it doesn't work. Here is my code for that

var regex = new RegExp('/^(?=.*[a-z])+(?=.*[A-Z])+(?=.*[0-9@#$-/:-?{-~!"^_`\[\]])+((?!' + $scope.username + ').)*$/');

I feel like I may just be missing something in translation/transition, but can't seem to get it right. TIA

1
  • 1
    Unrelated, but personally if just check the string for ccarn outside of the regex. Might even just check for the presence of the character classes in question in a less-complex way. I'm a big fan of regexes, but sometimes they're hard to think about and troubleshoot. Commented Jul 15, 2015 at 22:38

3 Answers 3

3

When you use the new RegExp() constructor to construct a regex from a string, you shouldn't include the leading and trailing / within the string. The /.../ form is only to be used when specifying a regex literal, which isn't what you're doing here.

When you do, say, var r = new RegExp('/foo/'), the regex you're actually getting is equivalent to doing var r = /\/foo\//, which clearly isn't what you want. So your constructor should actually look like this:

var regex = new RegExp('^(?=.*[a-z])+(?=.*[A-Z])+(?=.*[0-9@#$-/:-?{-~!"^_`\[\]])+((?!' + $scope.username + ').)*$');
//                     ↑↑                                                                                       ↑↑
// no "/" at the locations pointed to above

You probably also need to double your backslashes (since backslashes are escape characters in strings, but not in regex literals). So, [0-9@#$-/:-?{-~!"^_`\[\]] needs to become [0-9@#$-/:-?{-~!"^_`\\[\\]].

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

Comments

1

If you look closely the '/' character gets delimited when you give it inside the quotes so essentially the

var regex = new RegExp('/^(?=.*[a-z])+(?=.*[A-Z])+(?=.*[0-9@#$-/:-?{-~!"^_`\[\]])+((?!' + $scope.username + ').)*$/');

The regular expression would be like this

/\/^(?=.*[a-z])+(?=.*[A-Z])+(?=.*[0-9@#$-\/:-?{-~!"^_`[]])+((?!ccarn).)*$\//

The right way to go is to remove the '/' character from the RegEx and it should work

var regex = new RegExp('/^(?=.*[a-z])+(?=.*[A-Z])+(?=.*[0-9@#$-/:-?{-~!"^_`\[\]])+((?!' + $scope.username + ').)*$/');

The output for the above would be

/^(?=.*[a-z])+(?=.*[A-Z])+(?=.*[0-9@#$-\/:-?{-~!"^_`[]])+((?!ccarn).)*$/

which is exactly what you need ?

Hope it helps

Comments

0

Please before doing anything else, read a regex tutorial!

Mistakes:

  1. A lookahead is a zero width assertion ( in other words, it's only a test and doesn't match anything ), putting a quantifier for a zero width assertion doesn't make any sense: (?=.*[a-z])+ (it is like repeating something empty, zero or more times. Note that the regex engine will protest if you write something like this.)

  2. When you use the oop syntax to define a pattern (ie:var pattern = new RegExp("...), you don't need to add delimiters. You need to put double backslashes instead simple backslashes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.