I am looking for a javascript regex for whitespace. I am checking several different string in a loop and I need to locate the strings that have the big white space in them.
The white space string is built in a loop, like this...
please read this code as var whitespace = " "
then the loop just concats more non breaking spaces on it.
var whitespace = " "
for (var x = 0; x < 20; x++) {
whitespace += " "
}
then it is used later on in a string concat.
sometext += whitespace + someData;
I need to identify the strings that contain whitespace (20 spaces).
Or should I just be doing a contains(whitespace)
or something similar.
Any help is appreciated.
Cheers, ~ck in San Diego
The \s metacharacter matches whitespace character. Whitespace characters can be: A space character. A tab character.
\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.
The most common forms of whitespace you will use with regular expressions are the space (␣), the tab (\t), the new line (\n) and the carriage return (\r) (useful in Windows environments), and these special characters match each of their respective whitespaces.
Trimming Whitespace You can easily trim unnecessary whitespace from the start and the end of a string or the lines in a text file by doing a regex search-and-replace. Search for ^[ \t]+ and replace with nothing to delete leading whitespace (spaces and tabs). Search for [ \t]+$ to trim trailing whitespace.
For this specific question, Yogi is correct--you don't need regex and you're probably better off without it.
For future reference, if anyone else comes looking, regex has a special character for whitespace:
\s
In JS parlance (where regex literals may be enclosed in forward slashes) if you're looking for 20 spaces (not tabs, line feeds, etc.) you can do this:
/ {20}/
You'll also want to note that many browser regex engines do not consider the non-breaking space to be whitespace. The unicode representation for the NBSP character is:
\u00A0
Combined, looking for any combination of 20 whitespace characters (including tabs, etc.) OR NBSPs in a row (the square brackets denote a 'character class'):
/[\s\u00A0]{20}/
EDIT: Incorporating thomas' point about NBSP. Give his comment and/or answer an up-vote if you would--I know I have.
At least IE and Chrome do NOT include char 160 (nbsp) in \s. A regex that includes it is
wsregex = /[\s\u00A0]/
see also http://www.adamkoch.com/2009/07/25/white-space-and-character-160/ who reports about IE, but meanwhile chrome apparently also changed to not include it.
If you have defined whitespace as 20 spaces, you can do
var input = whitespace + someData;
if(input.indexOf(whitespace) != -1)
{
//input contains whitespace.
}
There is no need to use regex when you can do without it.
For IE (I tested in IE7), \s
does not match
but Chrome and Firefox does. So if you want to eliminate white spaces but not
(no break space), do not use \s
as it is not browser safe.
For example, you want to eliminate all white spaces including \n \t but not
, you can use \n \t
and white space, that is: str.replace(/[\t \n]+/g, "")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With