String.prototype.trim removes whitespace from both ends of a string and returns the result, leaving the middle untouched. It's what you run on user input before validating it, so that " yes " and "yes" are treated the same.
Implement stringTrim(str). Strip every whitespace character from the start and the end — spaces, tabs, newlines, and Unicode whitespace like the non-breaking space — but keep any whitespace that sits between non-whitespace characters.
function stringTrim(str) {
// returns str with leading and trailing whitespace removed;
// interior whitespace is preserved.
}
stringTrim(' hello '); // 'hello'
stringTrim('\t\n hi \n'); // 'hi' — tabs and newlines count too
stringTrim(' hello world '); // 'hello world' — interior spaces kept
stringTrim(' \t '); // '' — all-whitespace trims to empty
\t), newlines (\n), carriage returns (\r), and Unicode whitespace (e.g. the non-breaking space ) all count.''.'' unchanged.trimStart removes only the leading side, trimEnd only the trailing side. This question does both.You'll strip whitespace off both ends of a string with a single regular expression — the subtlety is matching every kind of whitespace, not just the space bar.
A user types " hello " into a field, or a copy-paste drags in a trailing newline. Before you compare, store, or validate that text, you want the padding gone — but only the padding at the ends, not the spaces inside a phrase like "hello world". That's trim. You're rebuilding it as stringTrim(str).
A string has three zones: a run of whitespace at the front, the meaningful content, and a run of whitespace at the back. trim deletes the front and back runs and returns the middle exactly as it was — including any spaces within the content.
The obvious regex targets spaces at the start and end:
function stringTrimNaive(str) {
return str.replace(/^ +| +$/g, '');
}
This handles literal spaces, and nothing else. Real whitespace is a whole family — tabs, newlines, carriage returns, and Unicode oddities like the non-breaking space — and a literal ' ' in the pattern matches none of them. So stringTrimNaive('\t\nhi\n') returns '\t\nhi\n' unchanged: the tab and newlines aren't spaces, so the pattern skips right over them.
function stringTrim(str) {
// \s matches the full whitespace family: space, tab, \n, \r, \f, \v, and
// Unicode whitespace like the non-breaking space and BOM. Anchored to the
// start (^) and end ($), with the g flag so BOTH ends are replaced.
return str.replace(/^\s+|\s+$/g, '');
}
module.exports = { stringTrim };
The whole fix is \s in place of the literal space. \s is the regex whitespace class, and in JavaScript it covers every whitespace character trim cares about. The pattern has two alternatives: ^\s+ matches a run of whitespace anchored to the start, \s+$ a run anchored to the end. The g flag matters — without it, replace would stop after the first match and clean only the leading side. Because both anchors require the whitespace to touch an edge, interior whitespace is never matched.
Take stringTrim(' hello world '):
^\s+ matches the two leading spaces. They're replaced with ''.\s+$ matches the two trailing spaces. They're replaced with ''.hello and world touch neither anchor (^ nor $), so neither alternative matches them. They stay.Result: 'hello world' — ends cleaned, middle intact. And stringTrim(' \t\n '): the entire string is one whitespace run, so ^\s+ matches all of it and replaces it with '', giving ''.
/^ +| +$/ leaves '\t' and '\n' in place. Use \s, which matches the whole whitespace family.g flag trims only one side — without g, replace stops after the first match (^\s+), so the trailing whitespace survives. You need g to hit both alternatives.trim('a b') is 'a b', not 'ab'. The ^ and $ anchors are what keep the middle safe; a bare /\s+/g would wrongly delete interior spaces too.trimStart / trimEnd — need just one side? Drop the other alternative: /^\s+/ for start, /\s+$/ for end.Keep practising the same patterns with a nearby challenge.
No submissions yet
Share your approach and start the discussion.
String.prototype.trim removes whitespace from both ends of a string and returns the result, leaving the middle untouched. It's what you run on user input before validating it, so that " yes " and "yes" are treated the same.
Implement stringTrim(str). Strip every whitespace character from the start and the end — spaces, tabs, newlines, and Unicode whitespace like the non-breaking space — but keep any whitespace that sits between non-whitespace characters.
function stringTrim(str) {
// returns str with leading and trailing whitespace removed;
// interior whitespace is preserved.
}
stringTrim(' hello '); // 'hello'
stringTrim('\t\n hi \n'); // 'hi' — tabs and newlines count too
stringTrim(' hello world '); // 'hello world' — interior spaces kept
stringTrim(' \t '); // '' — all-whitespace trims to empty
\t), newlines (\n), carriage returns (\r), and Unicode whitespace (e.g. the non-breaking space ) all count.''.'' unchanged.trimStart removes only the leading side, trimEnd only the trailing side. This question does both.You'll strip whitespace off both ends of a string with a single regular expression — the subtlety is matching every kind of whitespace, not just the space bar.
A user types " hello " into a field, or a copy-paste drags in a trailing newline. Before you compare, store, or validate that text, you want the padding gone — but only the padding at the ends, not the spaces inside a phrase like "hello world". That's trim. You're rebuilding it as stringTrim(str).
A string has three zones: a run of whitespace at the front, the meaningful content, and a run of whitespace at the back. trim deletes the front and back runs and returns the middle exactly as it was — including any spaces within the content.
The obvious regex targets spaces at the start and end:
function stringTrimNaive(str) {
return str.replace(/^ +| +$/g, '');
}
This handles literal spaces, and nothing else. Real whitespace is a whole family — tabs, newlines, carriage returns, and Unicode oddities like the non-breaking space — and a literal ' ' in the pattern matches none of them. So stringTrimNaive('\t\nhi\n') returns '\t\nhi\n' unchanged: the tab and newlines aren't spaces, so the pattern skips right over them.
function stringTrim(str) {
// \s matches the full whitespace family: space, tab, \n, \r, \f, \v, and
// Unicode whitespace like the non-breaking space and BOM. Anchored to the
// start (^) and end ($), with the g flag so BOTH ends are replaced.
return str.replace(/^\s+|\s+$/g, '');
}
module.exports = { stringTrim };
The whole fix is \s in place of the literal space. \s is the regex whitespace class, and in JavaScript it covers every whitespace character trim cares about. The pattern has two alternatives: ^\s+ matches a run of whitespace anchored to the start, \s+$ a run anchored to the end. The g flag matters — without it, replace would stop after the first match and clean only the leading side. Because both anchors require the whitespace to touch an edge, interior whitespace is never matched.
Take stringTrim(' hello world '):
^\s+ matches the two leading spaces. They're replaced with ''.\s+$ matches the two trailing spaces. They're replaced with ''.hello and world touch neither anchor (^ nor $), so neither alternative matches them. They stay.Result: 'hello world' — ends cleaned, middle intact. And stringTrim(' \t\n '): the entire string is one whitespace run, so ^\s+ matches all of it and replaces it with '', giving ''.
/^ +| +$/ leaves '\t' and '\n' in place. Use \s, which matches the whole whitespace family.g flag trims only one side — without g, replace stops after the first match (^\s+), so the trailing whitespace survives. You need g to hit both alternatives.trim('a b') is 'a b', not 'ab'. The ^ and $ anchors are what keep the middle safe; a bare /\s+/g would wrongly delete interior spaces too.trimStart / trimEnd — need just one side? Drop the other alternative: /^\s+/ for start, /\s+$/ for end.Keep practising the same patterns with a nearby challenge.
No submissions yet
Share your approach and start the discussion.