Regex Case Insensitive with input match

这一生的挚爱 提交于 2021-02-10 12:42:58

问题


I am trying to make validation for an html input by assigning via javascript a regex using the following code:

$("#"+field.field_id).attr("data-validation",'custom');
$("#"+field.field_id).attr("data-validation-regexp",field.regex);

If I set the regex to

^(Server (\d{1,2}))$

Then it works only with input 'Server ..' But i want the input Server to work with case insensitive. Like 'SERVER' or 'sErver'...... I tried to put ^(Server (\d{1,2}))$/i but it doesn't work. Any ideas ?


回答1:


You can use:

^([Ss][Ee][Rr][Vv][Ee][Rr] (\d{1,2}))$

to make it case insensitive.

There is no support of i (ignore case flag) in input pattern since it is compiled with the global, ignoreCase, and multiline flags disabled.




回答2:


This doesn't work, because you're not using open slash for regex:

^(Server (\d{1,2}))$/i

But this should work:

/^(Server (\d{1,2}))$/i


来源:https://stackoverflow.com/questions/27858038/regex-case-insensitive-with-input-match

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!