简体   繁体   中英

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

["

For this specific question, Yogi is correct--you don't need regex and you're probably better off without it.<\/i>

\s
["

At least IE and Chrome do NOT include char 160<\/strong> (nbsp) in \\s<\/strong> .<\/i>

wsregex = /[\s\u00A0]/

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 &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, "")

text.replaceAll(/(\s[a-zA-Z]) /gm, '$1&nbsp;')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM