Regex: Leading and trailing whitespace
Regex to trim whitespace: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.
Quick answer
Use /^\s+|\s+$/g with the global flag to find every match. In " hello world " it finds " ", " ". Most languages have a built-in trim (String.prototype.trim, Python’s str.strip) that is clearer and faster.
Last updated · Maintained by FreeAIToolz · How we test
/^\s+|\s+$/g1 match(es)
How this regex works
| Part | Meaning |
|---|---|
^\s+ | whitespace at the start |
| | or |
\s+$ | whitespace at the end |
Flags: g finds every match instead of stopping at the first.
Examples
| Input | Matches |
|---|---|
hello world | , |
no-spaces | none |
Every example above is checked automatically in JavaScript and Python each time the site is built.
Limitations and edge cases
Most languages have a built-in trim (String.prototype.trim, Python’s str.strip) that is clearer and faster. The regex is useful in editors and tools that only offer find-and-replace.
When to use this pattern
Replace matches with an empty string to trim, e.g. text.replace(/^\s+|\s+$/g, "").
Use it in your language
JavaScript
const matches = text.match(/^\s+|\s+$/g) ?? [];Python
import re
matches = [m.group(0) for m in re.finditer(r'^\s+|\s+$', text)]PHP
preg_match_all('~^\\s+|\\s+$~', $text, $m);
$matches = $m[0];Java
Matcher m = Pattern.compile("^\\s+|\\s+$").matcher(text);
while (m.find()) { System.out.println(m.group()); }Go
re := regexp.MustCompile(`^\s+|\s+$`)
matches := re.FindAllString(text, -1)More regex patterns
- Regex for Email address
- Regex for URL
- Regex for Domain name
- Regex for IPv4 address
- Regex for IPv4 CIDR block
- Regex for IPv6 address
- Regex for MAC address
- Regex for YouTube video URL
- Regex for US phone number
- Regex for International phone number (E.164)
- Regex for US ZIP code
- Regex for UK postcode
- Regex for Canadian postal code
- Regex for Date (YYYY-MM-DD)
- Regex for Date (MM/DD/YYYY)
- Regex for Date (DD/MM/YYYY)
- Regex for Time (24-hour)
- Regex for Time (12-hour with AM/PM)
- Regex for ISO 8601 date-time
- Regex for UUID
- Regex for MongoDB ObjectId
- Regex for URL slug
- Regex for Username
- Regex for Strong password
- Regex for Credit card number
- Regex for Semantic version (SemVer)
- Regex for Git commit hash
- Regex for JSON Web Token (JWT)
- Regex for Base64 string
- Regex for AWS access key ID
- Regex for Ethereum address
- Regex for Vehicle identification number (VIN)
- Regex for ISBN-13
- Regex for IBAN
- Regex for US Social Security number format
- Regex for Integer
- Regex for Numbers only
- Regex for Decimal number
- Regex for Number with thousands separators
- Regex for US dollar amount
- Regex for Percentage
- Regex for Alphanumeric
- Regex for Letters only
- Regex for Hex color code
- Regex for RGB / RGBA color
- Regex for Image file name
- Regex for Unix file path
- Regex for Windows file path
- Regex for Latitude and longitude
- Regex for X (Twitter) username
- Regex for Extract numbers from text
- Regex for Multiple spaces
- Regex for Repeated words
- Regex for HTML tags
- Regex for HTML comments
- Regex for Markdown links
- Regex for Hashtags
- Regex for @mentions
- Regex for Text in double quotes
- Regex for File extension