473,403 Members | 2,338 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,403 software developers and data experts.

Striping spaces from the beginning and end of a string through RegExp

I'm trying to create PHP trim() function equivalent using pure JS code
that truncates spaces from the begging and from the end of the given
string. One of the solutions is to use two "while" loops that will run
through the chars from the beginning and then on the end and cut
spaces. Instead I want to write a RegExp to accomplish this. Here is
the sample to give you an idea of my problem:

var str = new String(" and; n4m ")
var patternTrim = /(^\s+)(.*)(\s+$)/;
var arr = str.match(patternTrim);

Note the space char in the middle of the evaluated string. The
resulting string i want to have looks like this "and; n4m"

Jul 16 '06 #1
3 2385
wrote in news:11*********************@m79g2000cwm.googlegro ups.com in
comp.lang.javascript:
I'm trying to create PHP trim() function equivalent using pure JS code
that truncates spaces from the begging and from the end of the given
string. One of the solutions is to use two "while" loops that will run
through the chars from the beginning and then on the end and cut
spaces. Instead I want to write a RegExp to accomplish this. Here is
the sample to give you an idea of my problem:

var str = new String(" and; n4m ")
var patternTrim = /(^\s+)(.*)(\s+$)/;
var arr = str.match(patternTrim);

Note the space char in the middle of the evaluated string. The
resulting string i want to have looks like this "and; n4m"

The trick is to match waht you want to remove (not the whole
string) and then use .replace():

function trim( s )
{
return s.replace( /^\s+|\s+$/g , "" );
}

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 16 '06 #2
ro********@gmail.com wrote:
I'm trying to create PHP trim() function equivalent using pure JS code
that truncates spaces from the begging and from the end of the given
string. One of the solutions is to use two "while" loops that will run
through the chars from the beginning and then on the end and cut
spaces. Instead I want to write a RegExp to accomplish this. Here is
the sample to give you an idea of my problem:

var str = new String(" and; n4m ")
var patternTrim = /(^\s+)(.*)(\s+$)/;
var arr = str.match(patternTrim);

Note the space char in the middle of the evaluated string. The
resulting string i want to have looks like this "and; n4m"
First, do not use new String in JavaScript. It doesn't do what you expect. Use
use string literals.

String.prototype.trim = function () {
return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
});

var str = " and; n4m ";
var arr = str.trim();

http://javascript.crockford.com/remedial.html
Jul 16 '06 #3
ro********@gmail.com said the following on 7/16/2006 6:30 PM:
I'm trying to create PHP trim() function equivalent using pure JS code
that truncates spaces from the begging and from the end of the given
string.
<URL: http://jibbering.com/faq/#FAQ4_16>

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
10
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1:...
4
by: Marc Durufle | last post by:
I have a string like that " Vertices " and i want to obtain "Vertices" I would want to know if there is a simple way to put off spaces on a string, thank you -- Marc Durufle Inria...
3
by: Bernard Drolet | last post by:
Hi, I have a column containing a string; the string always starts with a letter (a-z), followed by an undefined number of letters, then one number or more. The REGEXP would look like *+ I...
7
by: Mindy Geac | last post by:
Is it possible to delete special caracters from a string and multiple spaces? When the input is : "a&*bbb cc/c d!d" I want the result : "abbb ccc dd" thnx. MJ <?php
5
by: Marco Gallo | last post by:
I've been trying to get rid of extra spaces in a table that I created with addresses in it. For instance in a field called TEST, I got the following address: " 1 main st Apt A "...
8
by: barry | last post by:
Have tried a number of things including space(n) in front of the string but when the DropDownList is shown the spaces have apparently been trimmed. Is this something that would have to be done in a...
7
by: Bosconian | last post by:
I know that str.replace(/^\s+|\s+$/g,''); will trim a string of space, but what about removing extra spaces from the middle? Where "hello world"
5
by: Andrew Wingorodov | last post by:
im made subj like this: inline bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; } str.erase ( std.begin () , std::find_if ( str.begin(), str.end(), isnt_space) );
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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,...
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
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,...
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...
0
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...
0
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,...
0
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...

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.