所以我读了这个:Regular expression works on regex101.com, but not on prod

我在antd中创建以下规则:Demo

    <Form.Item
      validateStatus={usernameError ? "error" : ""}
      help={usernameError || ""}
    >
      {getFieldDecorator("username", {
        rules: [
          { required: true, message: "Please input your username!" },
          {
            type: "regexp",
            pattern: new RegExp(
              /^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!#$%\-_=+<>])([a-zA-Z0-9!#$%\-_=+<>]+)$/
            ),
            message: `Password Pattern`
          }
        ]
      })(
        <Input
          prefix={<Icon type="user" style={{ color: "rgba(0,0,0,.25)" }} />}
          placeholder="Username"
        />
      )}
    </Form.Item>

正则表达式应匹配必须至少包含1个数字,1个字母和1个特殊字符的任何内容。

从日志中可以看到,正则表达式在JS中可以正常工作,但是在antd中,模式不起作用。

另外,我关注了this并正确添加了type="regexp"
还缺少什么?

最佳答案

您不必明确提及type: "regexp"

做这样的事情,它将起作用。

rules: [
 { required: true, message: "Please input your username!" },
 {
    pattern:/^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!#$%\-_=+<>])([a-zA-Z0-9!#$%\-_=+<>]+)$/,
    message: `Password Pattern`
 }
]

查看与regex相关的类似answer

10-06 05:26