RegEx: Character Classes XI ⁠- \s

Published by Isaac Pak in

You can think of character classes as characters with special meaning. They are recognized as special when you place the \ before the character.

Here are a list of the characters classes in JavaScript:

., \cX, \d, \D, \f, \n, \r, \s, \S, \t, \v, \w, \W, \0, \xhh, \uhhhh, \uhhhhh, [\b]

You probably know already know the string method trim(). It will remove all of the leading and trailing whitespaces in a string.

Create a regular expression that will function like the trim() method. Your regex will work together with this function: string.replace(REGEXP, ""). You must use the \s character class in your expression.

Example

const str = "    Hello World    "
// "Hello World"

const str = "    We need more space   "
// "We need more space"

Notes

Check the Resources tab for details on character classes if you're stuck.

Watch a quick demo on how Edabit works.