Empty Lines Regex

^\s*$

Match empty lines or lines containing only whitespace.

Test This Pattern →

Pattern Breakdown

Understanding the regex pattern ^\s*$:

  • ^ - Start of string
  • $ - End of string
  • \s - Whitespace character
  • * - Zero or more

Examples

Input Result Explanation
✓ Match Empty string
✓ Match Only spaces
text ✗ No Match Has content

Code Examples

JavaScript

JavaScript
const pattern = /^\s*$/;
const text = "test";

// Test if string matches
if (pattern.test(text)) {
  console.log("Valid!");
}

// Extract matches
const matches = text.match(pattern);
console.log(matches);

Python

Python
import re

pattern = r'^\s*$'
text = "test"

# Test if string matches
if re.match(pattern, text):
    print("Valid!")

# Find all matches
matches = re.findall(pattern, text)
print(matches)

PHP

PHP
<?php
$pattern = '/^\s*$/';
$text = "test";

// Test if string matches
if (preg_match($pattern, $text)) {
    echo "Valid!";
}

// Find all matches
preg_match_all($pattern, $text, $matches);
print_r($matches);
?>

Java

Java
import java.util.regex.*;

String pattern = "^\\s*$";
String text = "test";

Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(text);

if (m.matches()) {
    System.out.println("Valid!");
}

Use Cases

Common applications: Text processing, file cleanup

Frequently Asked Questions

What is the regex pattern for empty lines regex?
The regex pattern is: ^\s*$. Match empty lines or lines containing only whitespace.
How do I use the empty lines regex pattern in JavaScript?
In JavaScript: const regex = new RegExp('^\\s*$'); or const regex = /^\s*$/;
Does this pattern work in Python?
Yes, use: import re; pattern = re.compile(r'^\s*$') in Python. Most regex patterns are cross-language compatible.
What are common use cases for empty lines regex?
Text processing, file cleanup

Related Regex Patterns

More Regex Tools

GetDevUtils offers a suite of developer tools including: