在正则表达式中发出有效格式
Issue in regex for a valid format
我正在使用正则表达式模式匹配对字符串添加限制,但它不起作用,因为我在正则表达式方面经验不多。
就像不能没有 ( 没有 ) 或 [ 没有 ]。
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
我可能会使用允许的白名单来解决这个问题,而不是试图 运行 绕过并涵盖每一个负面案例。也就是说,使用以下正则表达式模式:
^\S+\.(?:(?:\(%\)|\[%\])%?)?$
一些JS代码:
var valid = "19.(%)";
if (/^\S+\.(?:(?:\(%\)|\[%\])%?)?$/.test(valid)) {
console.log("VALID: " + valid);
}
var invalid = "abc.(%";
if (!/^\S+\.(?:(?:\(%\)|\[%\])%?)?$/.test(invalid)) {
console.log("INVALID: " + invalid);
}
我正在使用正则表达式模式匹配对字符串添加限制,但它不起作用,因为我在正则表达式方面经验不多。 就像不能没有 ( 没有 ) 或 [ 没有 ]。
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
我可能会使用允许的白名单来解决这个问题,而不是试图 运行 绕过并涵盖每一个负面案例。也就是说,使用以下正则表达式模式:
^\S+\.(?:(?:\(%\)|\[%\])%?)?$
一些JS代码:
var valid = "19.(%)";
if (/^\S+\.(?:(?:\(%\)|\[%\])%?)?$/.test(valid)) {
console.log("VALID: " + valid);
}
var invalid = "abc.(%";
if (!/^\S+\.(?:(?:\(%\)|\[%\])%?)?$/.test(invalid)) {
console.log("INVALID: " + invalid);
}