我们需要判断用户输入的是否全是空格,可以使用以下方法:

方法一: 使用trim()

  1. /* 使用String.trim()函数,来判断字符串是否全为空*/
  2. function kongge1(test) {
  3. let str = test.trim();
  4. if (str.length == 0) {
  5. console.log('字符串全是空格');
  6. } else {
  7. console.log('输入的字符串为:' + test);
  8. }
  9. }
  1. 如果 trim() 不存在,可以在所有代码前执行下面代码
  2.  
  3. /* 给String原型链对象添加方法trim */
    if (!String.prototype.trim) {
    String.prototype.trim = function () {
    return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
    };
    }
  4.  
  5. 例如:
  6. /* 使用String.trim()函数,来判断字符串是否全为空*/
  7. function kongge1(test) {
  8.  
  9. /* 给String原型链对象添加方法trim */
  10. if (!String.prototype.trim) {
  11. String.prototype.trim = function () {
  12. return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  13. };
  14. }
  15.  
  16. let str = test.trim();
  17. if (str.length == 0) {
  18. console.log('字符串全是空格');
  19. } else {
  20. console.log('输入的字符串为:' + test);
  21. }
  22. }

方法二: 使用正则表达式

  1. /* 使用正则表达式来判断字符串是否全为空 */
  2. function kongge2(test) {
  3. if(test.match(/^\s+$/)){
  4. console.log("all space or \\n");
  5. }
  6. if(test.match(/^[ ]+$/)){
  7. console.log("all space")
  8. }
  9. if(test.match(/^[ ]*$/)){
  10. console.log("all space or empty")
  11. }
  12. if(test.match(/^\s*$/)){
  13. console.log("all space or \\n or empty")
  14. } else {
  15. console.log('输入的字符串为:' + test);
  16. }
  17. }

案例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>js判断字符串是否全为空(使用trim函数/正则表达式)</title>
  6. </head>
  7. <body>
  8. 姓名:<input type="text" name="userName" id='userName' onblur="check(value)" value="">
  9. <script type="text/javascript">
  10. /*
  11. typeof 检测给定变量的数据类型。
  12. 两种写法: typeof(value); typeof value;
  13. 可能返回的字符串:
  14. "undefined" --- 如果这个值未定义。
  15. "boolean" --- 如果这个值是布尔值。
  16. "string" --- 如果这个值是字符串。
  17. "number" --- 如果这个值是数值。
  18. "object" --- 如果这个值是对象或者null;
  19. "function" --- 如果这个值是函数。
  20. */
  21. let str = 'str';
  22. alert(typeof abc);//undefined;
  23. alert(typeof undefined);//undefined
  24. alert(typeof check(str));//undefined
  25. alert(typeof '');//string
  26. alert(typeof str);//string
  27. alert(typeof 98);//number
  28. alert(typeof {});//object
  29. alert(typeof null);//object
  30. function check(value) {
  31. if ('string' == typeof value) {
  32. kongge1(value);
  33. kongge2(value);
  34. } else {
  35. console.log(typeof value);
  36. console.log('请输入字符串');
  37. }
  38. }
  39. /* 使用String.trim()函数,来判断字符串是否全为空*/
  40. function kongge1(test) {
  41. let str = test.trim();
  42. if (str.length == 0) {
  43. console.log('字符串全是空格');
  44. } else {
  45. console.log('输入的字符串为:' + test);
  46. }
  47. }
  48. /* 使用正则表达式来判断字符串是否全为空 */
  49. function kongge2(test) {
  50. if(test.match(/^\s+$/)){
  51. console.log("all space or \\n");
  52. }
  53. if(test.match(/^[ ]+$/)){
  54. console.log("all space")
  55. }
  56. if(test.match(/^[ ]*$/)){
  57. console.log("all space or empty")
  58. }
  59. if(test.match(/^\s*$/)){
  60. console.log("all space or \\n or empty")
  61. } else {
  62. console.log('输入的字符串为:' + test);
  63. }
  64. }
  65. </script>
  66. </body>
  67. </html>
 
 

js判断字符串是否全为空(使用trim函数/正则表达式)的更多相关文章

  1. JS判断字符串是否全为中文

    //第一种代码(全为中文则返回"true",不全为中文则返回"false"): <script language="javascript&quo ...

  2. Js判断对象是否为空,Js判断字符串是否为空

    Js判断对象是否为空,Js判断字符串是否为空,JS检查字符串是否为空字符串 >>>>>>>>>>>>>>>&g ...

  3. 【最简单的方法】js判断字符串是否为JSON格式(20180115更新)

    前言 针对 “js判断字符串是否为JSON格式” 这个问题,在网上查了许多资料,都没找到自己想要的答案. 但是看到这个帖子<js判断字符串是否为JSON格式>后,突然灵光一闪,想到一种很简 ...

  4. Jquery 选择器 详解 js 判断字符串是否包含另外一个字符串

    Jquery 选择器 详解   在线文档地址:http://tool.oschina.net/apidocs/apidoc?api=jquery 各种在线工具地址:http://www.ostools ...

  5. JS判断字符串长度的5个方法

    这篇文章主要介绍了JS判断字符串长度的5个方法,并且区分中文和英文,需要的朋友可以参考下 目的:计算字符串长度(英文占1个字符,中文汉字占2个字符)   方法一:    代码如下: String.pr ...

  6. C++ 判断字符串是否全是数字

    在实际的工作中,需要提取程序中的字符串信息,但是程序中经常将一些数字当做字符串来进行处理,例如表盘的刻度信息,这时候就需要判断字符串是否全为数字,来进行真正意义上的字符串提取.下面介绍了判断字符串是否 ...

  7. Oracle中如何判断字符串是否全为数字,以及从任意字符串中提取数字

    本文介绍了判断字符串是否全为数字的4种办法,另外还介绍了一个translate函数的小技巧,从任意字符串中提取数字(调用2次translate函数).这个办法是一个公司同事发现的,用起来很方便,但理解 ...

  8. 【功能代码】---3 JS判断字符串是否包含某个字符串

    JS判断字符串是否包含某个字符串 var str ="abc"; if(str.indexOf("bc")>-1){ alert('str中包含bc字符串 ...

  9. [转]JS判断字符串是否为json数据

    原文地址:https://blog.csdn.net/qq_26400953/article/details/77411520 这周碰到了很多问题,尽量把遇到的问题都记录下来. JS判断字符串是否为j ...

随机推荐

  1. Jmeter正则提取器常用的几种方式

    使用jmeter的同学都知道,jmeter提供了各种各样的提取器,如jsonpath.Beanshell.Xpath.正则等!!! 我们就针对正则提取器如何使用进行说明. 举例说明:假设取sessio ...

  2. freemodbus移植讲解 ZZ

    一   为什么要移植Freemodbus 为什么要移植Freemodbus,这个问题需要从两个方面来回答.第一,modbus是一个非常好的应用层协议,它很简洁也相对完善.对于还没有接触过modbus的 ...

  3. Java中Comparable和Comparator你知多少?

    前言: 我喜欢这种遨游在Java的世界里,精心研究学习新鲜事物的感觉,即便再小再细再微不足道的东西,也让我乐此不疲,同时我也更愿意将我所会的东西分享出来供大家学习以及方便自己日后回顾.好了,闲话不多说 ...

  4. eclipse 中 Servlet 模板代码(其实是代码提示模板)

    说的是模板代码,应该说的是提示的模板代码,并不是一新建就会出现模板. 第一步:先建一个Servlet文件,写好自己想要的模板 我的模板如下: 全选并复制,等会要粘贴到Servlet的提示模板中. pa ...

  5. NumPy入门及基础

    1.1 NumPy 数组对象 NumPy中的ndarray是一个多维数组对象,该对象由两部分组成:  实际的数据;  描述这些数据的元数据. 大部分的数组操作仅仅修改元数据部分,而不改变底层的实际 ...

  6. react+webpack开发环境配置

    react是目前非常热门的前端框架,提倡组件化开发.所谓的组件,简单理解,就是一个独立的页面部件(包括页面模版,样式,逻辑等),它是一个独立的整体. webpack,是一个模块打包工具,其主要功能,就 ...

  7. Azure Messaging-ServiceBus Messaging消息队列技术系列7-消息事务

    上篇博文中我们介绍了Azure Messaging-ServiceBus Messaging消息回执机制. Azure Messaging-ServiceBus Messaging消息回执机制 本文中 ...

  8. Nginx编译参数

    configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector //设置额外的参数将被添加到CFLAGS变量.(FreeBSD或者 ...

  9. String 类的实现(5)String常用函数

      2 #include<iostream> 3 #include<stdio.h> 4 #include<assert.h> 5 #include <iom ...

  10. [SinGuLaRiTy] 高精度算法代码库

    [SinGuLaRiTy-1001] Copyrights (c) SinGuLaRiTy 2017. All Rights Reserved. 加法: #include<stdio.h> ...