Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript regex for whitespace or  

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 += "&nbsp;"
        }

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

like image 884
Hcabnettek Avatar asked Jul 22 '09 22:07

Hcabnettek


People also ask

What is /\ s +/ in JavaScript?

The \s metacharacter matches whitespace character. Whitespace characters can be: A space character. A tab character.

What does \s mean in regex?

\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.

How do you give a space in regex?

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.

How do I trim a whitespace in regex?

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.


4 Answers

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.

like image 196
steamer25 Avatar answered Sep 22 '22 12:09

steamer25


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.

like image 37
citykid Avatar answered Sep 25 '22 12:09

citykid


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.

like image 43
SolutionYogi Avatar answered Sep 21 '22 12:09

SolutionYogi


For IE (I tested in IE7), \s does not match &nbsp; but Chrome and Firefox does. So if you want to eliminate white spaces but not &nbsp;(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 &nbsp;, you can use \n \t and white space, that is: str.replace(/[\t \n]+/g, "")

like image 41
wcb1 Avatar answered Sep 21 '22 12:09

wcb1