By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,537 Members | 895 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,537 IT Pros & Developers. It's quick & easy.

trim string in javascript

P: n/a
for all your javascript trimming needs...

function trim(inputString) {
if (typeof inputString != "string") return inputString;
return inputString
//clear leading spaces and empty lines
.replace(/^(\s|\n|\r)*((.|\n|\r)*?)(\s|\n|\r)*$/g,"$2")

//take consecutive spaces down to one
.replace(/(\s(?!(\n|\r))(?=\s))+/g,"")

//take consecutive lines breaks down to one
.replace(/(\n|\r)+/g,"\n\r")

//remove spacing at the beginning of a line
.replace(/(\n|\r)\s/g,"$1")

//remove spacing at the end of a line
.replace(/\s(\n|\r)/g,"$1");
}

-- Jon
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 6 May 2004 07:48:53 -0700, Jon Kelling <sk******@mac.com> wrote:

[snip]
//clear leading spaces and empty lines
.replace(/^(\s|\n|\r)*((.|\n|\r)*?)(\s|\n|\r)*$/g,"$2")
Clears leading spaces, but not empty lines.
//take consecutive spaces down to one
.replace(/(\s(?!(\n|\r))(?=\s))+/g,"")
That does work, but will only do so in browsers that support look-ahead
assertions.
//take consecutive lines breaks down to one
.replace(/(\n|\r)+/g,"\n\r")
That works.
//remove spacing at the beginning of a line
.replace(/(\n|\r)\s/g,"$1")

//remove spacing at the end of a line
.replace(/\s(\n|\r)/g,"$1");


These two don't work.

It's clear that you haven't read the group FAQ. I suggest you do.

<URL:http://jibbering.com/faq/>

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #2

P: n/a
String.prototype.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, "");
}
and you call it like so:

strVal = strVal.trim()
cheers!
"Jon Kelling" <sk******@mac.com> wrote in message
news:a9**************************@posting.google.c om...
for all your javascript trimming needs...

function trim(inputString) {
if (typeof inputString != "string") return inputString;
return inputString
//clear leading spaces and empty lines
.replace(/^(\s|\n|\r)*((.|\n|\r)*?)(\s|\n|\r)*$/g,"$2")

//take consecutive spaces down to one
.replace(/(\s(?!(\n|\r))(?=\s))+/g,"")

//take consecutive lines breaks down to one
.replace(/(\n|\r)+/g,"\n\r")

//remove spacing at the beginning of a line
.replace(/(\n|\r)\s/g,"$1")

//remove spacing at the end of a line
.replace(/\s(\n|\r)/g,"$1");
}

-- Jon

Jul 23 '05 #3

P: n/a
I wrote my patterns quick under the idea that ^ and $ may match the end
of any line as opposed to the entire string input in some cases. Without
the time at work to dedicate to testing in all cases, I felt it safe to
use (.|\n|\r) in the middle to account for that (rendering the last
group fairly useless, but oh well). The rest of the patterns i wrote
quickly to cleanup any unnecessary data that I don't want to be storing,
worked for me, and thought I would include them on here. If the above
scenario is not a problem for all multiline input strings given
different browsers and especially platforms, it would be cool to have
confirmation on that, otherwise, it works fine for me.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.