Javascript regex for validating filenames

后端 未结 6 1662
失恋的感觉
失恋的感觉 2020-12-01 11:09

I have a regexp to validate file names. Here is it:

/[0-9a-zA-Z\\^\\&\\\'\\@\\{\\}\\[\\]\\,\\$\\=\\!\\-\\#\\(\\)\\.\\%\\+\\~\\_ ]+$/

It

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 12:07

    You need to add a starting anchor:

    /^[0-9a-zA-Z ... ]+$/
    

    This tells the engine to match from the start of the input all the way to the end of the input, whereas for your original expression it only needs to match at the end of the input.

提交回复
热议问题