Replace Whitespace Characters in String

O

o_swas

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!!!
 
S

Stephen Chalmers

o_swas said:
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/Core_JavaScript_1.5_Reference:Objects:RegExp
 
O

o_swas

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!!
 
R

RobG

o_swas said:
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>

[...]
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top