2018-09-30 23:01:58 +01:00
---
id: 587d7dbb367417b2b2512bac
title: Remove Whitespace from Start and End
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301362
2021-01-13 03:31:00 +01:00
dashedName: remove-whitespace-from-start-and-end
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
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.
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.
2020-11-27 19:02:05 +01:00
**Note:** The `String.prototype.trim()` method would work here, but you'll need to complete this challenge using regular expressions.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2021-03-02 16:12:12 -08:00
`result` should be equal to the string `Hello, World!`
2020-11-27 19:02:05 +01:00
```js
2021-04-09 14:34:55 +02:00
assert(result === 'Hello, World!');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
Your solution should not use the `String.prototype.trim()` method.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(!code.match(/\.?[\s\S]*?trim/));
```
2018-09-30 23:01:58 +01:00
2021-04-09 14:34:55 +02:00
The `result` variable should not directly be set to a string
2018-09-30 23:01:58 +01:00
```js
2021-04-09 14:34:55 +02:00
assert(!code.match(/result\s*=\s*["'`].*?["'` ]/));
2018-09-30 23:01:58 +01:00
```
2021-10-09 23:15:10 +05:30
The value of the `hello` variable should not be changed.
```js
assert(hello === ' Hello, World! ');
```
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
let hello = " Hello, World! ";
let wsRegex = /change/; // Change this line
let result = hello; // Change this line
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2019-02-12 20:34:52 +03:00
let hello = " Hello, World! ";
let wsRegex = /^(\s+)(.+[^\s])(\s+)$/;
let result = hello.replace(wsRegex, '$2');
2018-09-30 23:01:58 +01:00
```