Detect if string contains any spaces

后端 未结 5 691
醉酒成梦
醉酒成梦 2020-12-23 11:24

How do I detect if a string has any whitespace characters?

The below only detects actual space characters. I need to check for any kind of whitespace.



        
相关标签:
5条回答
  • 2020-12-23 11:59

    A secondary option would be to check otherwise, with not space (\S), using an expression similar to:

    ^\S+$
    

    Test

    function has_any_spaces(regex, str) {
    	if (regex.test(str) || str === '') {
    		return false;
    	}
    	return true;
    }
    
    const expression = /^\S+$/g;
    const string = 'foo  baz bar';
    
    console.log(has_any_spaces(expression, string));

    Here, we can for instance push strings without spaces into an array:

    const regex = /^\S+$/gm;
    const str = `
    foo
    foo baz
    bar
    foo baz bar
    abc
    abc abc
    abc abc abc
    `;
    let m, arr = [];
    
    while ((m = regex.exec(str)) !== null) {
    	// This is necessary to avoid infinite loops with zero-width matches
    	if (m.index === regex.lastIndex) {
    		regex.lastIndex++;
    	}
    
    	// Here, we push those strings without spaces in an array
    	m.forEach((match, groupIndex) => {
    		arr.push(match);
    	});
    }
    console.log(arr);


    If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


    RegEx Circuit

    jex.im visualizes regular expressions:

    0 讨论(0)
  • 2020-12-23 12:02
    var inValid = new RegExp('^[_A-z0-9]{1,}$');
    var value = "test string";
    var k = inValid.test(value);
    alert(k);
    
    0 讨论(0)
  • 2020-12-23 12:03
    var string  = 'hello world';
    var arr = string.split(''); // converted the string to an array and then checked: 
    if(arr[i] === ' '){
       console.log(i);
    }
    

    I know regex can do the trick too!

    0 讨论(0)
  • 2020-12-23 12:15

    What you have will find a space anywhere in the string, not just between words.

    If you want to find any kind of whitespace, you can use this, which uses a regular expression:

    if (/\s/.test(str)) {
        // It has any kind of whitespace
    }
    

    \s means "any whitespace character" (spaces, tabs, vertical tabs, formfeeds, line breaks, etc.), and will find that character anywhere in the string.

    According to MDN, \s is equivalent to: [ \f\n\r\t\v​\u00a0\u1680​\u180e\u2000​\u2001\u2002​\u2003\u2004​\u2005\u2006​\u2007\u2008​\u2009\u200a​\u2028\u2029​​\u202f\u205f​\u3000].


    For some reason, I originally read your question as "How do I see if a string contains only spaces?" and so I answered with the below. But as @CrazyTrain points out, that's not what the question says. I'll leave it, though, just in case...

    If you mean literally spaces, a regex can do it:

    if (/^ *$/.test(str)) {
        // It has only spaces, or is empty
    }
    

    That says: Match the beginning of the string (^) followed by zero or more space characters followed by the end of the string ($). Change the * to a + if you don't want to match an empty string.

    If you mean whitespace as a general concept:

    if (/^\s*$/.test(str)) {
        // It has only whitespace
    }
    

    That uses \s (whitespace) rather than the space, but is otherwise the same. (And again, change * to + if you don't want to match an empty string.)

    0 讨论(0)
  • 2020-12-23 12:23
    function hasSpaces(str) {
      if (str.indexOf(' ') !== -1) {
        return true
      } else {
        return false
      }
    }
    
    0 讨论(0)
提交回复
热议问题