Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

javascript - How can I check if string contains characters & whitespace, not just whitespace?

What is the best way to check if a string contains only whitespace?

The string is allowed to contain characters combined with whitespace, but not just whitespace.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Instead of checking the entire string to see if there's only whitespace, just check to see if there's at least one character of non whitespace:

if (/S/.test(myString)) {
    // string is not empty and not just whitespace
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...