2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7dbb367417b2b2512bac
|
|
|
|
challengeType: 1
|
2020-08-04 15:14:01 +08:00
|
|
|
forumTopicId: 301362
|
2020-10-01 17:54:21 +02:00
|
|
|
title: 删除开头和结尾的空白
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-08-04 15:14:01 +08:00
|
|
|
<section id='description'>
|
|
|
|
有时字符串周围存在的空白字符并不是必需的。字符串的典型处理是删除字符串开头和结尾处的空格。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:14:01 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
编写一个正则表达式并使用适当的字符串方法删除字符串开头和结尾的空格。
|
|
|
|
<strong>注意:</strong><br><code>.trim()</code>方法在这里也可以实现同样的效果,但是你需要使用正则表达式来完成此项挑战。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: "<code>结果</code>应该等于<code>'Hello, World!'</code>。"
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(result == "Hello, World!");
|
2020-08-04 15:14:01 +08:00
|
|
|
- text: 你不应该使用<code>.trim()</code>方法。
|
|
|
|
testString: assert(!code.match(/\.trim\(.*?\)/));
|
|
|
|
- text: <code>结果</code>变量不应该设置为等于字符串。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(!code.match(/result\s*=\s*".*?"/));
|
2018-10-10 18:03:03 -04: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
|
2020-08-04 15:14:01 +08:00
|
|
|
let hello = " Hello, World! ";
|
|
|
|
let wsRegex = /^(\s+)(.+[^\s])(\s+)$/;
|
|
|
|
let result = hello.replace(wsRegex, '$2');
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-04 15:14:01 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|