invalid escape in pattern HTML/Javascript

前端 未结 1 1236
-上瘾入骨i
-上瘾入骨i 2020-11-30 12:47

I am trying to make an field that uses a pattern to check if the input is a valid windows file path.

The pattern I have is



        
相关标签:
1条回答
  • 2020-11-30 13:06

    You only need to escape the characters in the character class that must be escaped otherwise, you will always get this error.

    Use

    pattern="(?:\w:|\\)(\\[\w\s.()~!@#$%^&=+';,{}[\]-]+)+\.exe"
    

    See the JSFiddle

    Details:

    • The hyphen must be at the end of the character class
    • The ] inside the character class must be escaped
    • The [ and . must not be escaped
    • The : must never be esacaped, it is never a special character
    • [a-zA-Z0-9_] = \w
    • The pattern is always anchored by default, you need no ^ and $ anchors.
    0 讨论(0)
提交回复
热议问题