Jump to content

How to trim spaces inside data values


Recommended Posts

Hi there,

I'd like to do pretty much the same function Trim does in excel. Remove any extra spaces inside the data including trailing or leading spaces. I saw someone posted this is possible with "regular expressions"?

I can see in the guide book there is left, right or both trim, but not sure how I would go about making a regular expression for removing both and any extra spaces in the data itself.

Thanks much for any help!




Link to comment
Share on other sites

  • 5 months later...

The way I read your question is that you want to remove all leading and trailing spaces and then two or more spaces from inside your string. I'd think the following would do what you'd like.

function trimAll(x) {

x = x.replace(/^\s*|\s*$/g, "");

x = x.replace(/\s+/g, " ");

return x;


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...