需求
实现
const PasswordRegExp = new RegExp('^(?=.*[a-zA-Z])(?=.*\\d)[a-zA-Z\\d\\W]{8,63}$');
const AlphanumericRegExp = new RegExp('^(?=.*[a-zA-Z])(?=.*\\d)[!-~]+$');
const UpperLowerLetterRegExp = new RegExp('^(?=.*[A-Z])(?=.*[a-z])[!-~]+$');
const SymbolRegExp = new RegExp(
"^(?=.*[!\"#$%&''()\\^\\-@\\[;:\\],./|`{+*}<>?_])[!-~]+$",
);
options
- escape-string-regexp npm包
- 转换后端(java)直接丢过来的正则
- 下面是核心代码:
export default function escapeStringRegexp(string) {
if (typeof string !== 'string') {
throw new TypeError('Expected a string');
}
return string
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d');
}