如何检查字符串是否包含字符&空白,而不仅仅是空白? [英] How can I check if string contains characters & whitespace, not just whitespace?

查看:168
本文介绍了如何检查字符串是否包含字符&空白,而不仅仅是空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查字符串是否只包含空格的最佳方法是什么?

What is the best way to check if a string contains only whitespace?

允许字符串包含带有空格的组合字符,但不是只是空格。

The string is allowed to contain characters combined with whitespace, but not just whitespace.

推荐答案

而不是检查整个字符串以查看是否只有空格,只是检查空格中是否至少有一个字符:

Instead of checking the entire string to see if there's only whitespace, just check to see if there's at least one character of non whitespace:

if (/\S/.test(myString)) {
    // string is not empty and not just whitespace
}

这篇关于如何检查字符串是否包含字符&空白,而不仅仅是空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆