4b0
4b0

Reputation: 22323

regex for filename

i continue with this question.Its help and work.regex for special character.My regex pattern is:

var characterReg = /^\s*[a-zA-Z0-9,\s]+\s*$/;

it accepta-zA-z0-9 only.But i want a regex which also support underscore(_) and das(-).I am unable to modify regex pattern because it is out of my mind.Plz some one suggest a correct pattern for my situation.

Upvotes: 0

Views: 1448

Answers (1)

shift66
shift66

Reputation: 11958

var characterReg = /^\s*[a-zA-Z0-9_,\s-]+\s*$/;
- is a special character and means range if you write it between two chars. So you can escape it (\\-) or just put in the end of []'s content.
Actually you can write \\w instead of a-zA-Z0-9_.\\w is a word character (letters, digits, and underscores).
And also \s* in the end is unnecessary because there is a whitespace in your square braces and [\\w,\s-]+ will match this foo bar-123 wholly
I think this is better: var characterReg = /^\s*[\\w,\s-]+$/;

Upvotes: 1

Related Questions