const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^a-zA-Z\d<>"'\\|])(?!.*[<>"'\\|])/;
console.log(regex.test("AaBb123!@#"));
// output: true
public static void main(String[] args) {
Pattern compile = Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[^a-zA-Z\\d<>\"'\\\\|])(?!.*[<>\"'\\\\|])");
Matcher matcher = compile.matcher("AaBb123!@#");
System.out.println(matcher.matches());
// output: false
}
丢给 AI 也看不出什么问题,AI 的结论是两段代码输出都应该一样为 true
1
revlis7 65 天前
\d 多了一个斜杠
|
3
nagisaushio 65 天前 1
java 的 matcher.matches 是尝试匹配整个字符串吧?你在 java 的正则后面加个 .* 就对了
|
4
javaluo 65 天前
public static void main(String[] args) {
Pattern compile = Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[^a-zA-Z\\d<>\"'\\\\|])(?!.*[<>\"'\\\\|])"); Matcher matcher = compile.matcher("AaBb123!@#"); System.out.println(matcher.matches()); } |
5
cheese 65 天前
Pattern compile = Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[^a-zA-Z\\d<>\"'\\\\|])(?!.*[<>\"'\\\\|])");试试这段
|
6
revlis7 65 天前 1
3 楼应该是对的,或者用 matcher.find()
https://stackoverflow.com/questions/4450045/difference-between-matches-and-find-in-java-regex |
7
NeroKamin OP @nagisaushio #3 加上确实好使了。也就是说 js regexp.test 和 java matcher.matches 的意义是不一样的吗?
|