views:

753

answers:

2

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

+1  A: 

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.

SolutionYogi
Thanks Yogi!! By the way, how do you get my questions so fast? Whenever I ask a regex question, you answer it very quick. Is it twitter related or something? I was just curious. ~ck
Hcabnettek
There is a questions feed on SO. Just set your RSS reader to it. :-)
Chris Jester-Young
I get an email whenever you post a question! ;) Just kidding.I log in to SO every few hours and see if there are any questions which I can answer.
SolutionYogi
Also, I looked at your past questions and it seems like you haven't accepted answers for quite a few of them. It will be nice if you can mark the answer which helped you solve the problem.
SolutionYogi
I thought Twitter was likely a way of broadcasting the question. I need to sub to the RSS feed. Thanks guys!
Hcabnettek
+1  A: 

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}/

Combined, looking for 20 whitespace characters (including tabs, etc.):

/\s{20}/
steamer25