0

I have never got my head around using regular expressions, so I have a bit of an issue understanding why my below listed regex. doesn't only allow the below listed characters:

  • A-Z & a-z (upper & lower)
  • ÅÄÖ & åäö
  • 0-9 & _ (underscore)

Here's the regex:

/^[\s\da-0-9-zA-ZåäöÅÄÖ_]$/i

What am I doing wrong?

Industrial
  • 41,400
  • 69
  • 194
  • 289

5 Answers5

1

Because this a-0-9-z is in reverse order. Use this: ^[\s\da-zA-ZåäöÅÄÖ_]$ or this: (?i)^[\s\da-zåäö_]$ with ignore case option set to enabled.

Also you can use special character \w, which means: letters, digits, and underscores

In you regex you're using \d and 0-9. \d includes 0-9, see Explanation

Community
  • 1
  • 1
Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • @Industrial, What do you mean? – Kirill Polishchuk Jul 16 '11 at 13:21
  • Running the regexp you provided (`^[\s\da-zA-ZåäöÅÄÖ_]$`) with a string containing `&` or `&` doesn't filter `&` or `&` out as I need – Industrial Jul 16 '11 at 13:25
  • @Industrial, this `^[\s\da-zA-ZåäöÅÄÖ_]$` will match only string which have length of `1` character. If your string is `@` or `&`, this regex shouldn't match. But if your string is `whitespace`, `digit`, `a-zA-ZåäöÅÄÖ` or `_`, it will match. – Kirill Polishchuk Jul 16 '11 at 13:31
  • 1
    @Industrial, If input string is `a` it will match, if input is `aa` it won't match. If you need to match string, which contains only these chars, you need to set `+` (one or more repetitions), i.e.: `^[\s\da-zA-ZåäöÅÄÖ_]+$` – Kirill Polishchuk Jul 16 '11 at 13:35
0

Is the expression here a verbatim copy? Because in

/^[\s\da-0-9-zA-ZåäöÅÄÖ_]$/i 

you have

a-0-9-zA-Z

where you probably mean

0-9a-zA-Z
SJuan76
  • 24,532
  • 6
  • 47
  • 87
0

the mistake is a-0-9-z which should be a-z0-9

furthermore, you can lose the 0-9 cause you used \d, and you can use \w for any "word" character.

yoavmatchulsky
  • 2,962
  • 1
  • 17
  • 22
0

This regex will match a signle character string and case insensitive in the following : any space character, any digit (0-9), any letter (a-ZA-Z), and one of åäöÅÄÖ_ .

0

Dunno about the latin, but you don't need 0-9 if you use \d, also, you don't need a-zA-Z if you use the /i flag, that makes everything case insensitive so you could leave just a-z.

casraf
  • 21,085
  • 9
  • 56
  • 91