gpt4 book ai didi

Javascript 检查字符串是否仅包含 nbsp;

转载 作者:行者123 更新时间:2023-12-03 00:11:34 27 4
gpt4 key购买 nike

这可能非常微不足道,但我正在寻找一种方法来检查字符串是否仅包含 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;');

如果您还想允许空字符串,请将 +(重复该组一次或多次)更改为 *(重复该组零次或多次)次)。

关于Javascript 检查字符串是否仅包含 nbsp;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54702064/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com