js正则表达式必需包含数字和大小写字母
时间: 2024-01-28 13:03:50 浏览: 105
可以使用正则表达式来匹配必须包含数字和大小写字母的字符串,例如:
```
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/
```
这个正则表达式使用了正则表达式的“零宽度正预测先行断言”(positive lookahead assertion),它的意思是:
- `(?=.*\d)`:必须包含至少一个数字
- `(?=.*[a-z])`:必须包含至少一个小写字母
- `(?=.*[A-Z])`:必须包含至少一个大写字母
- `.*`:匹配任意字符(包括换行符),重复任意次数
这个正则表达式可以用在 JavaScript 中的 `test` 方法上,例如:
```javascript
const regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/
console.log(regex.test('abc123')) // true
console.log(regex.test('AbC123')) // true
console.log(regex.test('abc')) // false
console.log(regex.test('123')) // false
console.log(regex.test('ABC')) // false
```
阅读全文
相关推荐


















