JS | RegEx WhiteSpace nbsp
by Zoltan Boros
HTML
This code replaces whitespaces in strings using the <code>\s</code> regex metacharacter.<br />
The input string that will be processed conatins an ordinary space, non-breaking space (<code>&nbsp;</code> or in hex <code>\xA0</code>), tab, line feed and carriage return.<br />
Run this code in different browsers in order to see what white space characters are involved in the <code>\s</code> regex metacharacter for each different browser.
<pre id="out">
</pre>
CSS
html {
font-family: sans-serif;
}
JavaScript
var out = document.getElementById("out");
function print(string)
{
out.innerHTML += string;
}
function replace(s)
{
return s.replace(/\s/g, "-");
}
var s = "Foo Bar\xa0Baz\tFoo\nBar\rBaz";
print("Original: [" + s + "]\n");
print("Modified: [" + replace(s) + "]");