javascript - 包含比 a 更多 b 的字符串的 Javascript 正则表达式
问题描述
我正在尝试在 Javascript 中构造一个正则表达式,使字符串包含的 b 比 a 多。可以在任何地方包含其他字符,但 b 必须多于 a。有人可以帮忙吗?
解决方案
最简单的路线:
let moreAsThanBs = (str) => str.match(/a/ig).length > str.match(/b/ig).length;
console.log(moreAsThanBs("Are there more As than Bs in this sentence?"));
console.log(moreAsThanBs("Are there more As than BBBBBs in this sentence?"));
推荐阅读
- asp.net-web-api - 如何从 webapi 获取数据集作为响应
- typescript - Typescript - 如何获取类型化数组?
- angular - 类型“[]”上不存在属性“名称”
- react-native - 如何在 React Native 中使用多个 Stack Navigator?
- php - 更新实体对象时无法在 foreach 循环外调用 entityManager->flush()
- canjs - 如何从动态生成的表单元素中获取值
- kotlin - 发行人赎回另一方持有的可替代代币
- reactjs - 未捕获的类型错误:无法读取未定义的属性“setState”
- python-3.x - pyqt5 的 pyinstaller 错误。无法运行分发文件。加载两组 Qt 二进制文件?
- php - 如何测试生成的 php 文件?