473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

trim string in javascript

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
3 7004
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Reply Via Newsgroup | last post by:
Folks, In PHP and some other scripting languages, one has trim() - It removes newline, tabs and blank spaces that might prefix, or suffix a string. Can someone tell me how I can do this in...
3
by: mhk | last post by:
Hi, i used Trim(my_val) and its giving error in JavaScript. is it a correct way to trim. Please suggest me, thanks in advance.
5
by: Alex Vassiliev | last post by:
Hi all. Just wanted to share two handy RegEx expressions to strips leading and trailing white-space from a string, and to replace all repeated spaces, newlines and tabs with a single space. *...
27
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM?...
7
by: Henri | last post by:
Hello, any idea why a regexp-based trim function would work fine in firefox but not in ie? code is String.prototype.trim = function() { re = /\s*$/g; return this.replace(re, "");
4
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft...
5
by: aswath | last post by:
i used the below code for trimming.. var str = " blah blah "; var trimmed = str.replace(/^\s+|\s+$/g, '') ; ...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.