0

How to remove all spaces from string using javascript. I have a code like this--

var Title = "";
var Str="g g";
Title = Str.replace(/^\s+|\s+$/g, '');
this gives me result Title=g g
how to get result Title=gg

4 Answers 4

2

Use following regexp:

Str.replace(/\s+/g, '');

The one you have right now just strippes spaces from start or end of your string.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

Title = Str.replace(/ /g, '');

Comments

0

Change code to:

var title = Str.replace(/\s+/g, '');

Your original regular expression would trim spaces only from the beginning or the end of the string.

Comments

0

You can just use:

var Str="g g";
Str.replace(" ", "");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.