Leading/Trailing Whitespace
beginnerMatches leading and trailing whitespace for trimming.
whitespacetrimcleanupformatting
Pattern
/^\s+|\s+$/gmTry It
0 of 5 strings matched
Explanation
Matches whitespace (spaces, tabs, etc.) at the start or end of each line. Useful for trim operations with regex replace.
Test Strings
Matching
- leading spaces
- trailing spaces
- tab indented
Non-matching
- no-whitespace
- internal spaces only
Language Compatibility
| Language | Support |
|---|---|
| JS | Full support |
| PYTHON | Full support |
| JAVA | Full support |
| PHP | Full support |
| GO | Full support |
Code Snippets
const regex = /^\s+|\s+$/gm;
const text = "your text here";
const matches = text.match(regex);
console.log(matches);