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

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

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

问题

你可以如下所示制定正则表达式模式,允许以下字符、数字、空格、双引号、?、!、逗号、连字符、点、@ 和 &:

  1. /^[a-zA-Z0-9\s"?!,&.@&-]+$/

然后,将该正则表达式模式应用到你的代码中。

英文:

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

  1. <textarea
  2. id="comments"
  3. type="textarea"
  4. placeholder='comments'
  5. {...register("comments", {
  6. required: true,
  7. minLength: {
  8. value: 5,
  9. message: "Minimum length of 5 letters"
  10. },
  11. pattern: {
  12. value: /^[a-z0-9]+$/i,
  13. message: "Supports characters, numbers, white space, "", ?, !, @, & and commas"
  14. }
  15. })}
  16. >
  17. </textarea>

答案1

得分: 1

将代码中的以下部分进行翻译:

  1. value: /^[a-z0-9]+$/i,

修改为:

  1. value: /^[a-z0-9\s"?!,\-.@&]+$/i,

\s 表示空格,需要对 - 进行转义,以免它被解释为范围分隔符。其余部分是你要允许的其他字符。

英文:

Change

  1. value: /^[a-z0-9]+$/i,

to

  1. 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:

确定