473,480 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Regular Expression To Take Out Whitespace Not Working

I've noticed that the following regular expression is offered pretty
frequently on message boards and on various and sundry websites in
conjuction with the string class replace function:

str.replace(/^\s*|\s*$/g,"")

I'm trying to use it and it is only stripping off leading whitespaces,
not trailing.

Does anyone have any insights?

thanks,
Geoff

Feb 6 '06 #1
6 1624
geoffrobinson wrote:
I've noticed that the following regular expression is offered pretty
frequently on message boards and on various and sundry websites in
conjuction with the string class replace function:

str.replace(/^\s*|\s*$/g,"")

I'm trying to use it and it is only stripping off leading whitespaces,
not trailing.
Which user agent (navigator.userAgent) and script engine (IE: ScriptEngine()
+ " " + new Array(ScriptEngineMajorVersion(), ScriptEngineMinorVersion(),
ScriptEngineBuildVersion).join(".")) have you tested with?
Does anyone have any insights?


WFM. Maybe you forgot the `g' flag or used the `m' flag.
PointedEars
Feb 6 '06 #2
geoffrobinson wrote on 06 feb 2006 in comp.lang.javascript:
I've noticed that the following regular expression is offered pretty
frequently on message boards and on various and sundry websites in
conjuction with the string class replace function:

str.replace(/^\s*|\s*$/g,"")

I'm trying to use it and it is only stripping off leading whitespaces,
not trailing.

Does anyone have any insights?


Works fine here, I suppose you forgot the leading "str ="

<script type='text/javascript>

var s = ' qwert asdf rr '

s = s.replace(/^\s*|\s*$/g,'')

alert('>>>'+s+'<<<')

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 6 '06 #3
geoffrobinson wrote:
[...] the string class replace function:


There are no classes in implemented J(ava)Script, those are
object-oriented languages using prototype-based inheritance.
It is String.prototype.replace() inherited by String objects
through the prototype chain, for example.
PointedEars
Feb 6 '06 #4
I figured out the problem. I was getting &nbsp; html characters from
the input text line, which have the ascii value of 160.

This changed since last week, so I'm thinking some sort of upgrade on
my machine may have caused a change in behavior.

Thanks for the advice. The regular expression engine didn't change or
anything.

Feb 6 '06 #5
JRS: In article <11**********************@f14g2000cwb.googlegroups .com>
, dated Mon, 6 Feb 2006 08:57:33 remote, seen in
news:comp.lang.javascript, geoffrobinson <ge***********@gmail.com>
posted :
I've noticed that the following regular expression is offered pretty
frequently on message boards and on various and sundry websites in
conjuction with the string class replace function:
The Web, etc., is full of trash. Why bother with copying it when you
can look in the newsgroup FAQ, as everyone should do before posting?
Note in particular sections 2.3 & 4.16.
str.replace(/^\s*|\s*$/g,"")

I'm trying to use it and it is only stripping off leading whitespaces,
not trailing.

Does anyone have any insights?


It works for me. But I'd use not \s* but \s+ though; your method
calls for a replace at each end whether or not there is whitespace
there.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 6 '06 #6
JRS: In article <11**********************@g43g2000cwa.googlegroups .com>
, dated Mon, 6 Feb 2006 15:32:34 remote, seen in
news:comp.lang.javascript, geoffrobinson <ge***********@gmail.com>
posted :
I figured out the problem. I was getting &nbsp; html characters from
the input text line, which have the ascii value of 160.


<FAQENTRY> ?

BTW, ASCII is a 7-bit code; your meaning is clear, your terminology
inexact. By the time that their value matters, they will be, AIUI,
Unicode \u00A0, which is 160 in 16 bits.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
Feb 7 '06 #7

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

Similar topics

5
11718
by: Andrew Dixon - Depictions.net | last post by:
Hi Everyone. I have been working on some code that strips the HTML code out of an HTML page leaving just the text on the page. At the moment this is what I have: // Strip all tags...
1
4154
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
1585
by: peterbe | last post by:
I want to match a word against a string such that 'peter' is found in "peter bengtsson" or " hey peter," or but in "thepeter bengtsson" or "hey peterbe," because the word has to stand on its own....
3
202
by: Christoph Boget | last post by:
I'm trying to get a regular expression to work in JS. It appears to be working everywhere else I'm testing it (an app called Regex Coach and php) but I can't seem to get it to work in JS. What...
0
7037
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
7032
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
7076
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
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5321
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,...
1
4767
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...
0
4471
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...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
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 ...

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.