Md Nurul Afsar Pervez
Md Nurul Afsar Pervez

Reputation: 77

Vue Js - removing spaces from string

Let category = "Baby Dress"

I want it to be trimed by spaces and text into lowercase. the output as "babydress". I used the following code. but it returns "baby dress".

category.trim(" ").toLowerCase()

I need to understand why it is not doing as I expected and what are the ways to do it.

Upvotes: 0

Views: 14123

Answers (3)

butterfly
butterfly

Reputation: 56

You can do like this:

category.replace(/\s+/g, '').toLowerCase()

Upvotes: 2

Talmacel Marian Silviu
Talmacel Marian Silviu

Reputation: 1736

The trim method only removes spaces at the beginning of the string. What you need is replacing the spaces with nothing with the replace method using regex:

category.replace(/\s/g, "").toLowerCase();

Upvotes: 1

Duoro
Duoro

Reputation: 308

It's not working as expected because .trim function is used to remove whitespace from both sides of the string, not from in-between.

enter image description here

You can use

category.toLowerCase().split(" ").join("")

Here I'm making the letters lower case, splitting them and then joining them.

Upvotes: 5

Related Questions