# \[已解决\]正则表达式 - 删除开头和结尾的空白

**URL:** <https://forum.freecodecamp.org/t/topic/559781>\
**Category:** General\
**Tags:** chinese-中文\
**Created:** [October 3, 2022, 11:44am UTC](https://forum.freecodecamp.org/t/topic/559781 "2022-10-03T11:44:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Odelia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/odelia/32/284156_2.png) [@Odelia](https://forum.freecodecamp.org/u/Odelia)\
**Post date:** [October 3, 2022, 11:44am UTC](https://forum.freecodecamp.org/t/topic/559781/1 "2022-10-03T11:44:33Z")

</div>

**告诉我们发生了什么：**  
我用现在的代码实现了删除开头结尾多余空格的要求，但检测持续报错。。。我不知道问题出在哪里

**你目前的代码**

```javascript
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/; // 修改这一行
let result = hello.replace(wsRegex,""); // 修改这一行
console.log(result);

```

**你的浏览器信息：**

用户代理是： `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36`

**挑战：** 正则表达式 - 删除开头和结尾的空白

**挑战的链接：**

> **[freeCodeCamp.org](https://www.freecodecamp.org/chinese/learn/javascript-algorithms-and-data-structures/regular-expressions/remove-whitespace-from-start-and-end)**
>
> 免费学习编程

---

<div class="post-metadata">

**Author:** ![ruochuan](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ruochuan/32/286791_2.png) [@ruochuan](https://forum.freecodecamp.org/u/ruochuan)\
**Post date:** [October 3, 2022, 2:09pm UTC](https://forum.freecodecamp.org/t/topic/559781/2 "2022-10-03T14:09:09Z")

</div>

```js
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g; // 修改这一行 添加 g 修饰符
let result = hello.replace(wsRegex,""); // 修改这一行
console.log(result === 'Hello, World!'); // true

```

需要添加全局 g 匹配，不然匹配不全面。

另外获得帮助的地方，其实有答案~

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/4/b/2/4b2cc54bcbcf6521954c0a3b90d3db8747f184b6.png)

> [@freeCodeCamp Challenge Guide: Remove Whitespace from Start and End](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-remove-whitespace-from-start-and-end/301362):
>
> [Remove Whitespace from Start and End](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/regular-expressions/remove-whitespace-from-start-and-end) Problem Explanation To solve this challenge you simply have to create a regex string that matches any spaces at the beginning or at the end of the string. Hints Hint 1 Think of how you can select substrings at the beginning or end of a string. Hint 2 Think of how you can select whitespace Solutions
> 
> > **Solution 1 (Click to Show/Hide)**
> >
> > let hello = " Hello, World! "; let wsRegex = /^\s+|\s+$/g; // Change this line let result = hello.replace(wsRegex…

---

<div class="post-metadata">

**Author:** ![Odelia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/odelia/32/284156_2.png) [@Odelia](https://forum.freecodecamp.org/u/Odelia)\
**Post date:** [October 8, 2022, 4:58am UTC](https://forum.freecodecamp.org/t/topic/559781/3 "2022-10-08T04:58:32Z")

</div>

我看了答案，并测试了不加g实现的效果是一样一样的，所以提出了问题。。

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 7, 2025, 3:42pm UTC](https://forum.freecodecamp.org/t/topic/559781/4 "2025-01-07T15:42:19Z")

</div>


