检查字符串是否有空格

时间:2022-03-20 20:13:52

I'm trying to check if a string has white space. I found this function but it doesn't seem to be working:

我试着检查一个字符串是否有空格。我发现了这个函数,但它似乎不起作用:

function hasWhiteSpace(s) 
{
    var reWhiteSpace = new RegExp("/^\s+$/");

    // Check for white space
    if (reWhiteSpace.test(s)) {
        //alert("Please Check Your Fields For Spaces");
        return false;
    }

    return true;
}

By the way, I added quotes to RegExp.

顺便说一下,我在RegExp中添加了引号。

Is there something wrong? Is there anything better that I can use? Hopefully JQuery.

有什么错了吗?有更好的吗?希望JQuery。

4 个解决方案

#1


188  

You can simply use the indexOf method on the input string:

您可以简单地在输入字符串上使用indexOf方法:

function hasWhiteSpace(s) {
  return s.indexOf(' ') >= 0;
}

Or you can use the test method, on a simple RegEx:

或者您可以使用测试方法,在一个简单的RegEx:

function hasWhiteSpace(s) {
  return /\s/g.test(s);
}

This will also check for other white space characters like Tab.

这还将检查其他空白字符,如Tab。

#2


18  

Your regex won't match anything, as it is. You definitely need to remove the quotes -- the "/" characters are sufficient.

你的regex不会匹配任何东西,因为它是。你一定要去掉引号——“/”字符就足够了。

/^\s+$/ is checking whether the string is ALL whitespace:

/ ^ \ s + $ /检查字符串是否所有空格:

  • ^ matches the start of the string.
  • ^匹配字符串的开始。
  • \s+ means at least 1, possibly more, spaces.
  • \s+意味着至少有1个,甚至更多的空间。
  • $ matches the end of the string.
  • $匹配字符串的末尾。

Try replacing the regex with /\s/ (and no quotes)

尝试用/\s/(不带引号)替换regex

#3


1  

Here is my suggested validation:

以下是我的建议:

var isValid = false;

// Check whether this entered value is numeric.
function checkNumeric() {
    var numericVal = document.getElementById("txt_numeric").value;

    if(isNaN(numericVal) || numericVal == "" || numericVal == null || numericVal.indexOf(' ') >= 0) {
        alert("Please, enter a numeric value!");
        isValid = false;
    } else {
        isValid = true;
    }
}

#4


1  

This function checks for other types of whitespace, not just space (tab, carriage return, etc.)

该函数检查其他类型的空格,而不只是空格(tab、回车等)。

import some from 'lodash/fp/some'
const whitespaceCharacters = [' ', '  ',
  '\b', '\t', '\n', '\v', '\f', '\r', `\"`, `\'`, `\\`,
  '\u0008', '\u0009', '\u000A', '\u000B', '\u000C',
'\u000D', '\u0020','\u0022', '\u0027', '\u005C',
'\u00A0', '\u2028', '\u2029', '\uFEFF']
const hasWhitespace = char => some(
  w => char.indexOf(w) > -1,
  whitespaceCharacters
)

console.log(hasWhitespace('a')); // a, false
console.log(hasWhitespace(' ')); // space, true
console.log(hasWhitespace(' ')); // tab, true
console.log(hasWhitespace('\r')); // carriage return, true

If you don't want to use Lodash, then here is a simple some implementation with 2 s:

如果您不想使用Lodash,那么这里有一个简单的具有2s的实现:

const ssome = (predicate, list) =>
{
  const len = list.length;
  for(const i = 0; i<len; i++)
  {
    if(predicate(list[i]) === true) {
      return true;
    }
  }
  return false;
};

Then just replace some with ssome.

然后用ssome替换一些。

const hasWhitespace = char => some(
  w => char.indexOf(w) > -1,
  whitespaceCharacters
)

For those in Node, use:

对于节点内的用户,请使用:

const { some } = require('lodash/fp');

#1


188  

You can simply use the indexOf method on the input string:

您可以简单地在输入字符串上使用indexOf方法:

function hasWhiteSpace(s) {
  return s.indexOf(' ') >= 0;
}

Or you can use the test method, on a simple RegEx:

或者您可以使用测试方法,在一个简单的RegEx:

function hasWhiteSpace(s) {
  return /\s/g.test(s);
}

This will also check for other white space characters like Tab.

这还将检查其他空白字符,如Tab。

#2


18  

Your regex won't match anything, as it is. You definitely need to remove the quotes -- the "/" characters are sufficient.

你的regex不会匹配任何东西,因为它是。你一定要去掉引号——“/”字符就足够了。

/^\s+$/ is checking whether the string is ALL whitespace:

/ ^ \ s + $ /检查字符串是否所有空格:

  • ^ matches the start of the string.
  • ^匹配字符串的开始。
  • \s+ means at least 1, possibly more, spaces.
  • \s+意味着至少有1个,甚至更多的空间。
  • $ matches the end of the string.
  • $匹配字符串的末尾。

Try replacing the regex with /\s/ (and no quotes)

尝试用/\s/(不带引号)替换regex

#3


1  

Here is my suggested validation:

以下是我的建议:

var isValid = false;

// Check whether this entered value is numeric.
function checkNumeric() {
    var numericVal = document.getElementById("txt_numeric").value;

    if(isNaN(numericVal) || numericVal == "" || numericVal == null || numericVal.indexOf(' ') >= 0) {
        alert("Please, enter a numeric value!");
        isValid = false;
    } else {
        isValid = true;
    }
}

#4


1  

This function checks for other types of whitespace, not just space (tab, carriage return, etc.)

该函数检查其他类型的空格,而不只是空格(tab、回车等)。

import some from 'lodash/fp/some'
const whitespaceCharacters = [' ', '  ',
  '\b', '\t', '\n', '\v', '\f', '\r', `\"`, `\'`, `\\`,
  '\u0008', '\u0009', '\u000A', '\u000B', '\u000C',
'\u000D', '\u0020','\u0022', '\u0027', '\u005C',
'\u00A0', '\u2028', '\u2029', '\uFEFF']
const hasWhitespace = char => some(
  w => char.indexOf(w) > -1,
  whitespaceCharacters
)

console.log(hasWhitespace('a')); // a, false
console.log(hasWhitespace(' ')); // space, true
console.log(hasWhitespace(' ')); // tab, true
console.log(hasWhitespace('\r')); // carriage return, true

If you don't want to use Lodash, then here is a simple some implementation with 2 s:

如果您不想使用Lodash,那么这里有一个简单的具有2s的实现:

const ssome = (predicate, list) =>
{
  const len = list.length;
  for(const i = 0; i<len; i++)
  {
    if(predicate(list[i]) === true) {
      return true;
    }
  }
  return false;
};

Then just replace some with ssome.

然后用ssome替换一些。

const hasWhitespace = char => some(
  w => char.indexOf(w) > -1,
  whitespaceCharacters
)

For those in Node, use:

对于节点内的用户,请使用:

const { some } = require('lodash/fp');