Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道我可以使用以下内容来检查 JavaScript 中的字符串是否为空:
if(Message != '')
在这种情况下,我将如何检查字符串“消息”是否为空且不包含多个空格。例如:
' '
我需要使用正则表达式吗?
jQuery 不会取代 Javascript。您可以使用:
if (Message.replace(/\s/g, "").length > 0) { // Your Code }
话虽如此,如果你真的想要 jQuery 版本,试试这个:
if ($.trim(Message).length > 0) { // Your Code }
或者,只要您只针对 IE9+ 和现代浏览器,您就可以使用内置的修剪功能。
if (Message.trim().length > 0) { // Your Code }