By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,599 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,599 IT Pros & Developers. It's quick & easy.

Regular Expression To Take Out Whitespace Not Working

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.