regex 错误-模式属性值不是有效的正则表达式

6psbrbz9  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(120)

我收到错误

Pattern attribute value [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])? 
   is not a valid regular expression: Uncaught SyntaxError: 
Invalid regular expression: /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/v: 
Invalid character in character class

字符串
对于FormControl

<Form.Control 
    name="email" 
    onChange={updateInput} 
    type="email" 
    pattern="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?" 
    placeholder="Email" 
    aria-label="aria-email" 
    isInvalid = {formValidState.email === '' ? false : !formValidState.email } isValid={formValidState.email === '' ? false : formValidState.email } 
    required />


这个问题已经在几个地方得到了回答,但我仍然不知道上面的正则表达式有什么问题。我没有正确地转义或没有转义哪些字符?还是别的什么?

yptwkmov

yptwkmov1#

这两个斜杠需要转义,你可以使用反斜杠。

<Form.Control name="email" onChange={updateInput} type="email" pattern="[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?" placeholder="Email" aria-label="aria-email" isInvalid = {formValidState.email === '' ? false : !formValidState.email } isValid={formValidState.email === '' ? false : formValidState.email } required />

字符串

相关问题