Regex for leading and trailing whitespace
/^[ \t]+|[ \t]+$/gm
Matches
Examples
Note
With the m flag, ^ and $ work per line. Replace with nothing to trim every line.
Matches in the example
" "" ""\t"" "
Token by token
^the start of the string (or line, with the m flag)[ \t]+one of: a space or a tab, one or more times, as many as possible|or[ \t]+one of: a space or a tab, one or more times, as many as possible$the end of the string (or line, with the m flag)
Other patterns
- Email address
- URL
- IPv4 address
- UUID
- Hex color
- Date (YYYY-MM-DD)
- Time (24-hour)
- US phone number
- Phone number (E.164)
- US ZIP code
- Credit card number
- URL slug
- Username
- Strong password
- HTML tag
- Semantic version
- MAC address
- JSON Web Token
- Hashtag
- @mention
- Number
- Leading and trailing whitespace
- Repeated word
- Blank lines
- Markdown link
- Quoted string
- Domain name
- File extension
- Base64
- Trailing comma