473,606 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ 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 1636
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.user Agent) and script engine (IE: ScriptEngine()
+ " " + new Array(ScriptEng ineMajorVersion (), ScriptEngineMin orVersion(),
ScriptEngineBui ldVersion).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.javas cript:
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.prototyp e.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************ **********@f14g 2000cwb.googleg roups.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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 6 '06 #6
JRS: In article <11************ **********@g43g 2000cwa.googleg roups.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.demo n.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
11726
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 replacePattern = "<(.|\n)+?>"; pageHTML = pageHTML.replaceAll(replacePattern,""); //Remove any HTML specific characters (e.g. &quot; or &amp;)
1
4162
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 regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
4
1599
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. The following code works for a single word: def createStandaloneWordRegex(word): """ return a regular expression that can find 'peter' only if it's written alone (next to space, start of string, end of string, comma, etc) but
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 the regex is supposed to do is: <p></p> OR <br> OR <br/>
0
8009
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7939
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8078
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
6753
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5962
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3919
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3964
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2442
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
0
1285
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.