473,545 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(patte rnTrim);

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 2398
wrote in news:11******** *************@m 79g2000cwm.goog legroups.com in
comp.lang.javas cript:
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(patte rnTrim);

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********@gmai l.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(patte rnTrim);

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.prototyp e.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********@gmai l.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.javas cript 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
31157
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 will split myString up using the delimiter of 1 space so that
10
39328
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: domything() And the regexp search assuming no case restriction would be,
4
11183
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 Rocquencourt Tel : 01 39 63 56 27 --------------------------
3
6543
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 need to extract the letters at the beginning in an SQL query or in PL/SQL
7
35894
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
4066
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 " First I tried to use the "trim", "ltrim" and "rtrim" functions, these functions did work, but only for those spaces at the beginning and at
8
3277
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 custom control? I would like the spaces to be at the front end of the string " xxxxx". In vs.net I am doing a listitem(string) Any Help is...
7
16452
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
3593
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
7676
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. ...
0
7932
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...
1
7442
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...
1
5347
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...
0
4965
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3456
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1905
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
1
1032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
729
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.