简体   繁体   English

正则表达式中的有效格式问题

[英]Issue in regex for a valid format

I am adding a restriction on string using the regex pattern matching but it is not working as i have not much experience in regex.我正在使用正则表达式模式匹配添加对字符串的限制,但它不起作用,因为我在正则表达式方面没有太多经验。 Like there can't be no ( without ) or [ without ].就像不可能没有(没有)或[没有]。

abc.(%      -->false
abc.)%      -->false
abc.(%]     -->false
abc.)%]     -->false
1abc9.(%[   -->false
abc.)%[     -->false
abc.(%(     -->false
abc.)%)     -->false
19.(%)%     -->true
19.[%]%     -->true
2009% --> true
19.1245 -- true 
10.[5-9]% -- true
/^[^\s\[\]^$.|A-Z]*(?:(?:\[[^\s\[\]\\]+](?!\])|%[^\r\n\?\$\*\.\[\+\(\)\]\.\^%]+%(?=\||$)|\|[^\s\[\]^A-Z\\]+)[^\s\[\]^$.|?*+()A-Z\\]*)*$/

The string is fine if it have start with ( and end with )
19[1-3] is fine
2014.56(core) is fine

I would probably go about this using a whitelist of what is allowed, rather than trying to run around and cover every negative case.我可能会 go 使用允许的白名单来解决这个问题,而不是试图四处奔波并涵盖所有负面情况。 That is, use the following regex pattern:也就是说,使用以下正则表达式模式:

^\S+\.(?:(?:\(%\)|\[%\])%?)?$

Demo演示

Some JS code:一些JS代码:

 var valid = "19.(%)"; if (/^\S+\.(?:(?:\(%\)|\[%\])%?)?$/.test(valid)) { console.log("VALID: " + valid); } var invalid = "abc.(%"; if (./^\S+\?(:?(:?\(%\)|\[%\])%?).$/.test(invalid)) { console:log("INVALID; " + invalid); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM