模式HTML / Javascript中的无效转义 [英] invalid escape in pattern HTML/Javascript

查看:193
本文介绍了模式HTML / Javascript中的无效转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个使用模式的输入字段来检查输入是否是有效的Windows文件路径。
我的模式是

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

/^(?:[\w]\:|\\)(\\[a-zA-Z_\-\s0-9\.()~!@#$%^&=+';,{}\[\]]+)+\.(exe)$/g

但是当把它放入a的pattern属性时输入字段(在控制台中)

however when putting this into the pattern attribute of an input field it says (in console)


模式属性值
^(?:[\ w]:| \ )(\ [a-zA-Z_-\s0-9。()〜!@#$%^& = +';,{} []] +)+。(exe)$是
不是有效的正则表达式:未捕获的SyntaxError:无效的常规
表达式:
/ ^(?:[\ w]:| \)(\ [a-zA-Z_-\s0 -9。()〜!@#$%^& = +';,{} []] +)+。(exe)$ /:
无效转义

Pattern attribute value ^(?:[\w]:|\)(\[a-zA-Z_-\s0-9.()~!@#$%^&=+';,{}[]]+)+.(exe)$ is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /^(?:[\w]:|\)(\[a-zA-Z_-\s0-9.()~!@#$%^&=+';,{}[]]+)+.(exe)$/: Invalid escape

我尝试了几种不同的写法,但似乎都没有。

I have tried several different ways of writing this but none seem to work.

推荐答案

您只需要转义必须转义的字符类中的字符,否则,您将始终收到此错误。

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

U se

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

请参阅 JSFiddle

详细信息


  • 连字符必须在最后必须转义角色类中的字符类

  • ]

  • [不得转义

  • 绝对不能被删除,它绝不是特殊字符

  • [a-zA-Z0-9 _] = \ w

  • 模式始终默认为锚定,你不需要 ^ $ 锚点。

  • 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.

这篇关于模式HTML / Javascript中的无效转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆