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

Replace Whitespace Characters in String

P: n/a

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
Share this Question
Share on Google+
3 Replies


P: n/a

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

P: n/a

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

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

Replies have been disabled for this discussion.