473,705 Members | 6,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trim string in javascript

for all your javascript trimming needs...

function trim(inputStrin g) {
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 7017
On 6 May 2004 07:48:53 -0700, Jon Kelling <sk******@mac.c om> 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.******@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #2
String.prototyp e.trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, "");
}
and you call it like so:

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

function trim(inputStrin g) {
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
4736
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 javascript? Much appreciated, randell d.
3
1740
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
33314
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. * JavaScript example: String.prototype.trim = function() { // Strip leading and trailing white-space
27
6864
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? ----------------------------------------------------------------------- Using Regular Expressions (JavaScript 1.2/JScript 4+) : String.prototype.LTrim=new Function("return this.replace(/^\\s+/,'')") String.prototype.RTrim=new Function("return this.replace(/\\s+$/,'')") String.prototype.Trim= new...
7
2917
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
5516
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft ----------------------------------------------------------------------- Using Regular Expressions (JavaScript 1.2/JScript 3+) : String.prototype.trimLeft = function() {
5
17583
by: aswath | last post by:
i used the below code for trimming.. var str = " blah blah "; var trimmed = str.replace(/^\s+|\s+$/g, '') ; alert(trimmed); alert(trimmed.value.length); its aint working.. i think small error.. can any1 retify.. thanks in advance..
2
1100
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft ----------------------------------------------------------------------- Using Regular Expressions (JavaScript 1.2/JScript 3+) : String.prototype.trimLeft = function() {
1
1168
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft ----------------------------------------------------------------------- Using Regular Expressions (JavaScript 1.2/JScript 3+) : String.prototype.trimLeft = function() {
0
8768
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9139
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7895
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6606
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4704
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.