[英]Joi validation regex for a specific value or a list of specific values
I am converting CSV to JSON and I need to validate a cell value.我正在将 CSV 转换为 JSON 并且我需要验证单元格值。 Having a list of possible valid values
['abc', 'def', 'ref', 'rop']
.有一个可能的有效值列表
['abc', 'def', 'ref', 'rop']
。 I want to write a regex assuming a valid cell would be either我想写一个正则表达式,假设一个有效的单元格是
validValue='abc'
notValidValue='adr'
) orvalidValue='abc'
notValidValue='adr'
)或\\\
(eg validValue='abc \\\ ref \\\ def'
notValidValue='aef \\\ kop'
)\\\
分隔(例如validValue='abc \\\ ref \\\ def'
notValidValue='aef \\\ kop'
) I tried with Joi.string().regex(/(?:abc|def|ref|rop)(/s\\\\\\/s(?:abc|def|ref|rop))?/)
but it throws eeror for first /s
-> invalid character and also Argument of type RegExp is not assignable to parameter of type 'string | StringRegexOptions'....
我试过
Joi.string().regex(/(?:abc|def|ref|rop)(/s\\\\\\/s(?:abc|def|ref|rop))?/)
但是它会为第一个/s
-> 无效字符抛出错误,并且Argument of type RegExp is not assignable to parameter of type 'string | StringRegexOptions'....
Argument of type RegExp is not assignable to parameter of type 'string | StringRegexOptions'....
You have typo.你有错字。 The
/s
should be \s
. /s
应该是\s
。 And the tail ?
尾巴
?
will match the second part 0 or once.将匹配第二部分 0 或一次。 Change it to
*
can match as many time as it will.将其更改为
*
可以匹配任意多次。 or you want to specify the maximum {0,3}
.或者您想指定最大值
{0,3}
。
regex(/(?:abc|def|ref|rop)(\s\\\\\\\s(?:abc|def|ref|rop))*/)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.