HomeRegex LibraryLeading & Trailing Whitespace Regex
100% Client-Side β€’ 0 B Data Leaves Browser
formatting

Leading & Trailing Whitespace Regex

Targets superfluous leading and trailing spaces or tab characters across string lines for clean sanitization.

Regular Expression Pattern:
/^\s+|\s+$/g
Valid Match Example:
untrimmed developer input
Invalid / Non-Match Example:
clean input

Regex Syntax Breakdown & Explanation

  • β€’^\s+ : Matches spaces at beginning of line
  • β€’| : Logical OR operator
  • β€’\s+$ : Matches trailing whitespace before end of line

Implementation in Popular Languages

JavaScript / TypeScript
const regex = /^\s+|\s+$/g;
const isValid = regex.test("   untrimmed developer input   ");
console.log(isValid); // true
Python (re)
import re

pattern = r"^\s+|\s+$"
match = re.match(pattern, "   untrimmed developer input   ")
print(bool(match)) # True
Go (regexp)
package main
import (
  "fmt"
  "regexp"
)

func main() {
  re := regexp.MustCompile(`^\s+|\s+$`)
  fmt.Println(re.MatchString("   untrimmed developer input   "))
}
Frequently Asked Questions

Frequently Asked Questions

Everything you need to know regarding specifications, syntax, and security best practices.

The regular expression pattern is /^\s+|\s+$/g. Targets superfluous leading and trailing spaces or tab characters across string lines for clean sanitization.