Validation
Non-Empty Text Regex
A required field should reject both an empty value and a value made only of spaces, tabs or line breaks. This pattern accepts any text as long as at least one non-whitespace character appears somewhere in it. The class [\s\S] matches every character including newlines, so multi-line messages in a text area are accepted as a whole. It is the regex form of checking value.trim() !== "", which is useful in places that only accept a pattern, such as a schema definition or a configuration file. Leading and trailing spaces are still allowed; trim them separately before saving.
Pattern (JavaScript)
/^[\s\S]*\S[\s\S]*$/Starts at the start of the line/string, then one of the characters \s\S, zero or more times, then any character that is not whitespace, then one of the characters \s\S, zero or more times, then the end of the line/string.
Examples
Matches
- ✓ hello
- ✓ x
- ✓ line1 line2
Does not match
- ✗
- ✗
- ✗
Try it
1 match (JavaScript engine)
hello x line1 line2
Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[\s\S]*\S[\s\S]*$/g;
const text = "hello";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[\s\S]*\S[\s\S]*$')
text = 'hello'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[\s\S]*\S[\s\S]*$/';
$text = 'hello';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[\\s\\S]*\\S[\\s\\S]*$");
String text = "hello";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[\s\S]*\S[\s\S]*$`)
text := "hello"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[\s\S]*\S[\s\S]*$");
var text = "hello";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[\s\S]*\S[\s\S]*$").unwrap();
let text = "hello";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[\s\S]*\S[\s\S]*$/
text = 'hello'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why use [\s\S] instead of a dot?
- Without the s flag, the dot does not match newline characters. [\s\S] matches everything, so multi-line input works with no extra flags.
- Is there a simpler way in JavaScript?
- In code, value.trim().length > 0 does the same job. The regex form helps when a tool or schema only accepts a pattern.
- Does it reject non-breaking spaces only?
- Yes. In JavaScript, \s includes the non-breaking space and other Unicode spaces, so a value made only of them is rejected.