-2

I want to check to see if an item in an array/list of strings has 1 or more spaces and/or 1 or more line breaks.

What exactly would I check for in an if statement, if I wanted to check to see if the string item has this?

6
  • 1
    What code have you tried yet? Share with us. Commented Mar 25, 2016 at 3:54
  • 1
    The least you can do before asking a question is search: [javascript] string contains space Commented Mar 25, 2016 at 3:56
  • I've tried checking if the item was just a "\n", but that only works if it's one space. I've also tried "\n+" which I thought would mean multiple "\n"s, but it doesn't. I've researched a bit already, but couldn't find anyone else with this question. Commented Mar 25, 2016 at 3:57
  • This one pretty much answers your question: stackoverflow.com/q/17616624/218196 . Commented Mar 25, 2016 at 3:58
  • Maybe something like this: if (/\s*/.test("hello\n")) { alert("foo"); } Commented Mar 25, 2016 at 4:01

1 Answer 1

1

Possibly:

count = string.search(/\s/);

Where string is your input, \s is the regex for space characters, and count is the total number found in string.

Further info here:

javascript regex to count whitespace characters

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.