简体   繁体   中英

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 accept a-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.

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-]+$/;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM