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
2018-09-30 23:01:58 +01:00
---
## Description
< section id = '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.
< / section >
## Instructions
< section id = 'instructions' >
Write a regex and use the appropriate string methods to remove whitespace at the beginning and end of strings.
2020-02-12 11:47:54 +05:00
< strong > Note:< / strong > The < code > String.prototype.trim()< / code > method would work here, but you'll need to complete this challenge using regular expressions.
2018-09-30 23:01:58 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2018-10-20 21:02:47 +03:00
- text: < code > result</ code > should equal to < code > "Hello, World!"</ code >
2019-07-24 02:32:04 -07:00
testString: assert(result == "Hello, World!");
2020-02-12 11:47:54 +05:00
- text: Your solution should not use the < code > String.prototype.trim()</ code > method.
2020-03-26 20:13:34 +05:00
testString: assert(!code.match(/\.?[\s\S]*?trim/));
2018-10-04 14:37:37 +01:00
- text: The < code > result</ code > variable should not be set equal to a string.
2019-07-24 02:32:04 -07:00
testString: assert(!code.match(/result\s*=\s*".*?"/));
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
let hello = " Hello, World! ";
let wsRegex = /change/; // Change this line
let result = hello; // Change this line
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```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
```
2019-07-18 08:24:12 -07:00
2018-09-30 23:01:58 +01:00
< / section >