How can I make regex pattern with characters, numbers, white space, double quotes, ?, !, commas, hyphen, dots, @ and &

huangapple go评论90阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月4日 05:26:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定