Files
freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/regular-expressions/remove-whitespace-from-start-and-end.md
Sem Bauke 21c8500f94 fix(curriculum): prevent submitting array instead of string (#41720)
* fix(curriculum) prevent users from submitting an array instead of string

* use strict equality instead of new test

* Add suggestions from Tom

Co-authored-by: Tom <20648924+moT01@users.noreply.github.com>

Co-authored-by: Tom <20648924+moT01@users.noreply.github.com>
2021-04-09 07:34:55 -05:00

1.3 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7dbb367417b2b2512bac Remove Whitespace from Start and End 1 301362 remove-whitespace-from-start-and-end

--description--

Sometimes whitespace characters around strings are not wanted but are there. Typical processing of strings is to remove the whitespace at the start and end of it.

--instructions--

Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.

Note: The String.prototype.trim() method would work here, but you'll need to complete this challenge using regular expressions.

--hints--

result should be equal to the string Hello, World!

assert(result === 'Hello, World!');

Your solution should not use the String.prototype.trim() method.

assert(!code.match(/\.?[\s\S]*?trim/));

The result variable should not directly be set to a string

assert(!code.match(/result\s*=\s*["'`].*?["'`]/));

--seed--

--seed-contents--

let hello = "   Hello, World!  ";
let wsRegex = /change/; // Change this line
let result = hello; // Change this line

--solutions--

let hello = "   Hello, World!  ";
let wsRegex = /^(\s+)(.+[^\s])(\s+)$/;
let result = hello.replace(wsRegex, '$2');