2

Possible Duplicate:
Is there a way to make JSLint happy with this regex?

I'm just cleaning up my code using JShint and its throwing up an error for this piece of code.

if (password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]/)) { 
       score++;
    }

The error is Unescaped '^'.

Basically I want to give more points for a more complex password?

Community
  • 1
  • 1
chris
  • 605
  • 1
  • 9
  • 27
  • 3
    why do you have all those commas? – Evan Davis Jan 17 '13 at 14:50
  • JSLINT is right, you need to escape the ^ sign, ^ means not in Regex or start of sentence if not in a [] block – Benjamin Gruenbaum Jan 17 '13 at 15:06
  • @Mathletics I thought I had to comma delimit each character I presume I dont ? :D – chris Jan 17 '13 at 15:10
  • @BenjaminGruenbaum But it **is** in a `[]` block (and not at the beginning of it)... – Ian Jan 17 '13 at 15:12
  • @chris Nope, it will match any single character inside `[]` (with specific exceptions, of course) – Ian Jan 17 '13 at 15:12
  • @Ian apperantly you are right, I did not realize that "^".match(/[ ^]/) – Benjamin Gruenbaum Jan 17 '13 at 15:15
  • @BenjaminGruenbaum Yeah, it's news to me too. I guess I've never had to match that, so I never realized it – Ian Jan 17 '13 at 15:17
  • 1
    @BenjaminGruenbaum But if you look at the link for the possible duplicate that VisioN just commented with, it looks like JSLint thinks it's better to escape it in the middle of `[]`. So although it's not required, doesn't change the functionality, and still works, escaping `^` in the middle of `[]` seems to be the correct way according to JSLint – Ian Jan 17 '13 at 15:20

1 Answers1

5

In regex set ^ character means NOT, so for some reason (possibly in order to be on a safe side) JSHint asks you to escape it (same goes for dash - symbol):

/[!@#$%\^&*?_~\-£(),.]/

Also you should pay attention that there is no need in delimiting symbols with comma in sets.

You may read more information about regular syntax in MDN.

VisioN
  • 143,310
  • 32
  • 282
  • 281
  • 2
    Actually, the `^` is only special when it immediately follows the opening bracket. It's ok to have an unescaped `^` in the middle of the class. – georg Jan 17 '13 at 14:57
  • 1
    @thg435 I guess this is a problem of JSHint. – VisioN Jan 17 '13 at 14:59
  • 2
    `there is no need in delimiting symbols with comma in sets.` Rather than "no need", it is more accurate to say that the syntax is so. – nhahtdh Jan 17 '13 at 16:04