473,409 Members | 1,943 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Replace Whitespace Characters in String


Hello,

I have a JavaScript string. I want to replace all consecutive
occurrences of whitespace characters like spaces, tabs, newlines, and
form feeds with another string.

For example, say I have a string consisting of:

-- 3 spaces
-- The characters "hello"
-- 2 newline (\n) characters
-- The characters "goodbye"
-- 5 spaces

After applying some sort of regular expression to replace consecutive
occurrences of whitespace chars with the string "X", the string should
consist of the following:

-- The character "X"
-- The characters "hello"
-- The character "X"
-- The characters "goodbye"
-- The character "X"

How could I do this using regular expressions? I'm quite familiar with
JavaScript but don't know anything about regular expressions or using
them in JavaScript, so please show me step-by-step how it's done.

Thank you!!!

Oct 12 '05 #1
3 30384

o_swas <ry**********@gmail.com> wrote in message news:11*********************@g47g2000cwa.googlegro ups.com...

Hello,

I have a JavaScript string. I want to replace all consecutive
occurrences of whitespace characters like spaces, tabs, newlines, and
form feeds with another string.

For example, say I have a string consisting of:

-- 3 spaces
-- The characters "hello"
-- 2 newline (\n) characters
-- The characters "goodbye"
-- 5 spaces

After applying some sort of regular expression to replace consecutive
occurrences of whitespace chars with the string "X", the string should
consist of the following:

-- The character "X"
-- The characters "hello"
-- The character "X"
-- The characters "goodbye"
-- The character "X"

How could I do this using regular expressions? I'm quite familiar with
JavaScript ...


Then you should have little trouble understanding the information at the
address below, long before your assigment becomes due.

http://developer.mozilla.org/en/docs...Objects:RegExp

--
S.C.
Oct 12 '05 #2

Thanks for the link! However, I'm still having trouble figuring out
what the correct regular expression should be. Could you please help?

Thank you!!

Oct 12 '05 #3
o_swas wrote:
Hello,

I have a JavaScript string. I want to replace all consecutive
occurrences of whitespace characters like spaces, tabs, newlines, and
form feeds with another string.


In a regular expression, '\s' matches any whitespace character. Add the
'+' operator to make it match one or more of them, adding the 'g' flag
will make it match all instances. So:

var regExp = /\s+/g;
var aString = ' blah blah ';
alert( aString.replace(regExp,'X')); // --> XblahXblahX

Here's some play stuff:

<form action="">
<table>
<tr>
<td align="right">Replace whitespace with:</td>
<td><input type="text" name="newString" value="%"></td>
</tr><tr>
<td align="right">String:</td>
<td><textarea name="textString"
rows="20" cols="40"> Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Curabitur condimentum, orci vitae porttitor
tempus, nibh lectus viverra nunc, quis pretium turpis lacus
at magna . </textarea></td>
</tr><tr>
<td align="center"><input type="reset"></td>
<td align="center">
<input type="button" value="Replace whitespace" onclick="
var x = this.form.newString;
var y = this.form.textString;
y.value = y.value.replace(/\s+/g,x.value)
">
</td>
</tr>
</table>
</form>

[...]

--
Rob
Oct 12 '05 #4

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

Similar topics

4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
2
by: nadia | last post by:
Hi, I send an email from an asp.net application using the SmtpMail object. I create and HTML string and send the email in HTML MailFormat. I see the string in debug mode before the email is...
10
by: M Bourgon | last post by:
I'm trying to figure out how to find the last whitespace character in a varchar string. To complicate things, it's not just spaces that I'm looking for, but certain ascii characters (otherwise,...
12
by: Brian | last post by:
I want to use regxp to check that a form input contains at least 1 non-space charcter. I'd like to only run this if the browser supports it. For DOM stuff, I'd use if (documentGetElementById) {}...
9
by: David P. Donahue | last post by:
I'm using RegExp.Replace(string, string, string) to remove some pieces of large strings. But I seem to be having trouble getting it to match "all characters up to and including xxxxx" (which I...
3
by: David Pratt | last post by:
Hi. I am splitting a string on a non whitespace character. One or more whitespace characters can be returned as items in the list. I do not want the items in the list that are only whitespace (can...
14
by: inpuarg | last post by:
I want to find a & character using Regex. But not && How can i manage this in c# Quickfind window ? -------------------------------------------------- ne kadar yažarsan yaža sevdišin kadardżr...
11
by: Fabri | last post by:
I searched and tried to develop (with no luck) a function to do the following: I have a string that may be: "Le'ts go to <a href="my.htm">my car</a>. Tomorrow I'll have to buy a new car. My...
13
by: Chaim Krause | last post by:
I am unable to figure out why the first two statements work as I expect them to and the next two do not. Namely, the first two spit the sentence into its component words, while the latter two...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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
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...
0
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
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
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...
0
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,...
0
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
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...

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.