1.8 KiB

id, title, challengeType, forumTopicId, localeTitle
id title challengeType forumTopicId localeTitle
587d7dbb367417b2b2512bac Remove Whitespace from Start and End 1 301362 Удаление пробелов от начала и конца

Description

Иногда пробельные символы вокруг строк не нужны, но есть. Типичная обработка строк - удаление пробелов в начале и в конце.

Instructions

Напишите регулярное выражение и используйте соответствующие строковые методы для удаления пробелов в начале и в конце строк. Заметка
Здесь будет работать метод .trim() , но вам придется выполнить эту задачу, используя регулярные выражения.

Tests

tests:
  - text: <code>result</code> should equal to <code>"Hello, World!"</code>
    testString: assert(result == "Hello, World!");
  - text: You should not use the <code>.trim()</code> method.
    testString: assert(!code.match(/\.trim\(.*?\)/));
  - text: The <code>result</code> variable should not be set equal to a string.
    testString: assert(!code.match(/result\s*=\s*".*?"/));

Challenge Seed

let hello = "   Hello, World!  ";
let wsRegex = /change/; // Change this line
let result = hello; // Change this line

Solution

let hello = "   Hello, World!  ";
let wsRegex = /^(\s+)(.+[^\s])(\s+)$/;
let result = hello.replace(wsRegex, '$2');