# Regular Expressions - Remove Whitespace from Start and End | Simpler solution?

**URL:** <https://forum.freecodecamp.org/t/regular-expressions-remove-whitespace-from-start-and-end-simpler-solution/558449>\
**Category:** freeCodeCamp Contributors\
**Tags:** contributing\
**Created:** [September 28, 2022, 6:09pm UTC](https://forum.freecodecamp.org/t/regular-expressions-remove-whitespace-from-start-and-end-simpler-solution/558449 "2022-09-28T18:09:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gromoslaw.kroczka](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/gromoslaw.kroczka/32/242003_2.png) [@gromoslaw.kroczka](https://forum.freecodecamp.org/u/gromoslaw.kroczka)\
**Post date:** [September 28, 2022, 6:09pm UTC](https://forum.freecodecamp.org/t/regular-expressions-remove-whitespace-from-start-and-end-simpler-solution/558449/1 "2022-09-28T18:09:04Z")

</div>

Hello!

I’ve found solution for [Regular Expressions - Remove Whitespace from Start and End](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/remove-whitespace-from-start-and-end) Challenge which I think is a _little simpler_ than what is present as Solution in [Guide Page](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-remove-whitespace-from-start-and-end/301362).

I’ve checked it with multiple words, devided by multiple whitespaces and I think it works as it should. Am i right or am I missing something? Does .replace() has any kind of adventage in that case?

**FreeCodeCamp proposed solution:**

```js
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g; // Change this line
let result = hello.replace(wsRegex, ""); // Change this line

```

**My solution:**

```js
let hello = " Hello, World! ";
let wsRegex = /\S+.*\S+/; // Change this line
let result = hello.match(wsRegex)[0]; // Change this line

```

Thank you in advance for you comments!

---

<div class="post-metadata">

**Author:** ![sarabulho](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sarabulho/32/291673_2.png) [@sarabulho](https://forum.freecodecamp.org/u/sarabulho)\
**Post date:** [December 28, 2022, 6:23pm UTC](https://forum.freecodecamp.org/t/regular-expressions-remove-whitespace-from-start-and-end-simpler-solution/558449/2 "2022-12-28T18:23:12Z")

</div>

Fine! I follow the same trail using the array properties. It works, but checking the FCC solution, I figure that this challenge is to be solved using the resources of the previous lessons on the module. Hence, the use of replace. Which never occurred to me either! Cheers.

---

<div class="post-metadata">

**Author:** ![rodrigo-hlc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rodrigo-hlc/32/332481_2.png) [@rodrigo-hlc](https://forum.freecodecamp.org/u/rodrigo-hlc)\
**Post date:** [February 25, 2023, 10:12pm UTC](https://forum.freecodecamp.org/t/regular-expressions-remove-whitespace-from-start-and-end-simpler-solution/558449/3 "2023-02-25T22:12:47Z")

</div>

Since we’re on the topic of alternate solutions, I’ll add the one I came up with, which is even simpler and does uses the .replace method:

let hello = " Hello, World! ";  
let wsRegex = /\s\s+/g;  
let result = hello.replace(wsRegex, “”);

It simply tells the computer to replace all the instances in which there are 2 or more whitespaces with nothing, so it only focuses on the spaces at the start and end.  
But I realize this isn’t an ideal solution, as it wouldn’t work if there were multiple spaces one after the other within the sentence.

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [February 25, 2023, 11:20pm UTC](https://forum.freecodecamp.org/t/regular-expressions-remove-whitespace-from-start-and-end-simpler-solution/558449/4 "2023-02-25T23:20:45Z")

</div>

> [@rodrigo-hlc](#):
>
> let wsRegex = /\s\s+/g;

This may pass the test scenarios used in the challenge but it doesn’t work if you change the test string to

```auto
let hello = " Hello, World! ";

```

Now you only have one space at the beginning and end of the string and thus your regexp won’t remove them.

---

<div class="post-metadata">

**Author:** ![rodrigo-hlc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rodrigo-hlc/32/332481_2.png) [@rodrigo-hlc](https://forum.freecodecamp.org/u/rodrigo-hlc)\
**Post date:** [February 26, 2023, 8:46pm UTC](https://forum.freecodecamp.org/t/regular-expressions-remove-whitespace-from-start-and-end-simpler-solution/558449/5 "2023-02-26T20:46:23Z")

</div>

Ahh, I see. Good catch! Thank you!

---

<div class="post-metadata">

**Author:** ![8web](https://avatars.discourse-cdn.com/v4/letter/8/aeb1de/32.png) [@8web](https://forum.freecodecamp.org/u/8web)\
**Post date:** [March 14, 2023, 11:59pm UTC](https://forum.freecodecamp.org/t/regular-expressions-remove-whitespace-from-start-and-end-simpler-solution/558449/6 "2023-03-14T23:59:37Z")

</div>

Your solution can be fixed a bit, just use a [Negative lookbehind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet) statement and it will pass all freeCodeCamp tests.

```auto
	let hello = " Hello, World! ";
	let wsRegex =/(?<!,)\s+/g; // skip single space after symbol ,
	let result = hello.replace(wsRegex, '');

```

It can also be further modified to skip single spaces between words or numbers. It will look like this:

```auto
let hello = " Hello, World! RegEx is cool. ";
	let wsRegex = /(?<![,\!\w])\s+/g; // expanded by set [,\!\w]
	let result = hello.replace(wsRegex, '');
	console.log(result); // expect 'Hello, World! RegEx is cool.'

```

but still it is not a perfect solution, it has some drawbacks

---

<div class="post-metadata">

**Author:** ![rodrigo-hlc](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rodrigo-hlc/32/332481_2.png) [@rodrigo-hlc](https://forum.freecodecamp.org/u/rodrigo-hlc)\
**Post date:** [September 4, 2023, 10:02pm UTC](https://forum.freecodecamp.org/t/regular-expressions-remove-whitespace-from-start-and-end-simpler-solution/558449/7 "2023-09-04T22:02:18Z")

</div>

Hello everyone,

Because I went on vacation for about two months after completing the Javasctipt unit, I’ve started the fCC course again and, now that I’ve gotten to this particular challenge, I came up with a different solution that’s working for the test cases. Let me know what you think and if you can come up with a test case that would make my proposed solution wrong. Cheers!

```auto
let hello = " Hello, World! ";
let wsRegex = /\S+\s*\S+/g; // Change this line
let result = hello.match(wsRegex).join();

```

Basically, it matches everything that isn’t a whitespace, and accounts for the possible existence of whitespace inbetween words. And _now_ that I’ve written that, I realize it would not work if there were more words separated by whitespace…

So never mind! Still, I’m gonna leave this up here in case it comes in handy for anybody

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [December 18, 2023, 3:27am UTC](https://forum.freecodecamp.org/t/regular-expressions-remove-whitespace-from-start-and-end-simpler-solution/558449/10 "2023-12-18T03:27:08Z")

</div>


