javascript - Javascript 检查字符串是否仅包含 nbsp;
问题描述
这可能非常微不足道,但我正在寻找一种方法来检查字符串是否仅包含 html 实体nbsp;
例子:
// checking if string ONLY CONTAINS nbsp;
'nbsp;' -> true
'nbsp;nbsp;nbsp;nbsp;' -> true
'nbsp;nbsp; HELLO WORLD nbsp;' -> false
我该怎么做呢?显然,最简洁有效的方法是理想的……有什么建议吗?
解决方案
使用正则表达式:
const test = str => console.log(/^(?:nbsp;)+$/.test(str));
test('nbsp;');
test('nbsp;nbsp;nbsp;nbsp;');
test('nbsp;nbsp; HELLO WORLD nbsp;');
如果您也想允许空字符串,则将+
(重复组一次或多次)更改为*
(重复组零次或多次)。
推荐阅读
- powershell - 如何在 azure devops 中备份工件
- tableview - 为什么不返回表格单元格默认选择样式?斯威夫特 5.2
- laravel - SQLSTATE [42S22]:未找到列:1054 '字段列表'中的未知列'created_by'
- flutter - 如何在颤振上测试 blocconsumer
- scheme - 如何在 guile 中使用(Web 客户端)将响应数据作为字符串获取?
- java - 使用 Spring 集成的 WebSocket
- javascript - Discord.js 向服务器中的所有成员发送消息?
- loops - 如何在for循环中grep成功
- c# - 使用 CRUD 的 ASP.Net Core 和 EFCore 转换错误
- minecraft-forge - 创建输出红石信号的块的最佳方法是什么