Regular Expression To Take Out Whitespace Not Working

G

geoffrobinson

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
 
T

Thomas 'PointedEars' Lahn

geoffrobinson said:
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
 
E

Evertjan.

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>
 
T

Thomas 'PointedEars' Lahn

geoffrobinson said:
[...] 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
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Mon, 6 Feb 2006 08:57:33 remote, seen in
news:comp.lang.javascript said:
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.
 
G

geoffrobinson

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.
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Mon, 6 Feb 2006 15:32:34 remote, seen in
news:comp.lang.javascript said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top