Allow “/” forward slash in regular expression

后端 未结 2 1833
余生分开走
余生分开走 2020-12-11 17:20
var patt = /^(?=.*[a-zA-Z0-9.!@#&*\\-\\u0080-\\u052F])[a-zA-Z0-9\\s.!@#&*\',\\-\\u0080-\\u052F]*$/;
console.log(patt.test(\"\\u002f\"));

I

相关标签:
2条回答
  • 2020-12-11 18:11

    You can escape a / character, by using \/.

    Using unicode will actually result in the absolute same result, as using the character itself - and therefore will not solve your problem.

    0 讨论(0)
  • 2020-12-11 18:16

    It is easy to add a forward slash, just escape it. No need using any character references or entities.

    var patt = /^(?=.*[a-zA-Z0-9.!@#&*\-\u0080-\u052F])[\/a-zA-Z0-9\s.!@#&*',\-\u0080-\u052F]*$/;
                                                        ^                 
    

    var patt = /^(?=.*[a-zA-Z0-9.!@#&*\-\u0080-\u052F])[\/a-zA-Z0-9\s.!@#&*',\-\u0080-\u052F]*$/;
    alert(patt.test("/test"));

    0 讨论(0)
提交回复
热议问题