2

In my application i want to remove the unwanted space from starting and ending.And also if there have more than space between words also remove them leaving one space. How can i do this?

1

3 Answers 3

4

Try this:

//remove leading spaces
content = content.replace(/^\s+/g,'');
//remove trailing spaces
content = content.replace(/\s+$/g,'');
//remove whitespaces in the middle
content = content.replace(/\s+/g,' ');
Sign up to request clarification or add additional context in comments.

1 Comment

when i using this an error occurs "x.rep is undefined" x is my variable
1

This should work to remove leading and trailing spaces, and reduce two or more spaces to a single space:

str.replace(/^\s+|\s+$/, '').replace('/\s\s+/', ' ')

1 Comment

nah, removes only leading one
1

You can use jQuery trim method.This function remove all new lines and all spaces leading or trailing from input string.

var str = '          it is a test string                ';

alert($.trim(str));

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.