var str = "This string has too many spaces."; var result = str.replace(/(s| | )+/gi, "_"); document.write(result);
Tag Archive for whitespace
Replace space with _
Extension method to test whether a string is null or empty or whitespace
public static bool IsNullOrEmpty(this string input)
{
if (string.IsNullOrEmpty(input))
return true;
return string.IsNullOrEmpty(input.Trim());
}
Remove leading whitespace from a text file
$ cat file.txt | sed -e 's/^[ ]*//'
Converts big whitespaces into one whitespace
<?php
$string = 'This string has no whitespaces.';
echo ereg_replace( ' +', '', $string ); //note: before '+' we have 2 spaces
?>
Output: This string has no white spaces.
// OR EVEN BETTER
trim's code can of course be simplified with some use of the trim() function....
<?php
$str = " Words with lots of spaces ";
$str = preg_replace('/ss+/', ' ', trim($str));
?>
Doing the trim() first reduces the workload being put on the more expensive preg_replace().
Convert whitespace to underscores
Find (id=")(w+)(s) Replace $1$2_
Kruse’s hasClass
/*
** Matt Kruse's hasClass, with slight modification
** Determine if an object or class string contains a given class.
*/
function hasClass (obj, className) {
if (typeof obj == 'undefined' || obj==null || !RegExp) { return false; }
var re = new RegExp("(^|s)" + className + "(s|$)");
if (typeof(obj)=="string") {
return re.test(obj);
}
else if (typeof(obj)=="object" && obj.className) {
return re.test(obj.className);
}
return false;
}
Sed convert multiline whitespace to single newline
$ cat filename |sed '/^[ ]*$/{
N
/^
[ ]*$/d
}'
Trim Leading & Trailing Whitespace
function trimWhitespace(value) {
var result = value.match(/^s*(.*S)s*$/);
if (result !== null && result.length === 2)
return result[1];
else
return value;
};
CSS nowrap – Prevent Text From Wrapping in HTML
white-space:nowrap;