英文:
How can I make regex pattern with characters, numbers, white space, double quotes, ?, !, commas, hyphen, dots, @ and &
问题
你可以如下所示制定正则表达式模式,允许以下字符、数字、空格、双引号、?、!、逗号、连字符、点、@ 和 &:
/^[a-zA-Z0-9\s"?!,&.@&-]+$/
然后,将该正则表达式模式应用到你的代码中。
英文:
How can I make regex pattern allowing following characters, numbers, white space, double quotes, ?, !, commas, hyphen, dots, @ and &
<textarea
id="comments"
type="textarea"
placeholder='comments'
{...register("comments", {
required: true,
minLength: {
value: 5,
message: "Minimum length of 5 letters"
},
pattern: {
value: /^[a-z0-9]+$/i,
message: "Supports characters, numbers, white space, "", ?, !, @, & and commas"
}
})}
>
</textarea>
答案1
得分: 1
将代码中的以下部分进行翻译:
value: /^[a-z0-9]+$/i,
修改为:
value: /^[a-z0-9\s"?!,\-.@&]+$/i,
\s
表示空格,需要对 -
进行转义,以免它被解释为范围分隔符。其余部分是你要允许的其他字符。
英文:
Change
value: /^[a-z0-9]+$/i,
to
value: /^[a-z0-9\s"?!,\-.@&]+$/i,
\s
is whitespace, and -
needs to be escaped so it's not treated as the range separator. The rest is just the other characters you said you want to allow.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论