Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-24-2011, 05:29 AM   PM User | #1
CFEmma
New to the CF scene

 
Join Date: Aug 2010
Location: Alameda, California, USA
Posts: 6
Thanks: 7
Thanked 0 Times in 0 Posts
CFEmma is an unknown quantity at this point
Javascript Regex \S vs. \s

Hello, I have this piece of code I read from a book to remove text nodes that only have white space.

Code:
if (node.nodeType == 3 && ! /\S/.test(node.nodeValue)){
   // code to remove the text node
}
Why should we use this:
Code:
! /\S/.test(node.nodeValue)
Instead of this?
Code:
/\s/.test(node.nodeValue)
CFEmma is offline   Reply With Quote
Old 02-24-2011, 05:52 AM   PM User | #2
chump2877
Senior Coder

 
chump2877's Avatar
 
Join Date: Dec 2004
Location: the U.S. of freakin' A.
Posts: 2,530
Thanks: 15
Thanked 128 Times in 121 Posts
chump2877 is on a distinguished road
Because the second line of code will return true even if it contains non-space characters, i.e.:

Quote:
Some text
...would "fail" validation because there is a space between "Some" and "text", so the text node would be removed (when it shouldn't be)
__________________
Regards, R.J.

---------------------------------------------------------

Help spread the word! Like my YouTube-to-Mp3 Web Conversion Software on Facebook !! :)

Last edited by chump2877; 02-24-2011 at 05:56 AM..
chump2877 is offline   Reply With Quote
Users who have thanked chump2877 for this post:
CFEmma (02-25-2011)
Old 02-24-2011, 07:57 AM   PM User | #3
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
difference between \s and \S
bullant is offline   Reply With Quote
Users who have thanked bullant for this post:
CFEmma (02-25-2011)
Old 02-24-2011, 08:25 AM   PM User | #4
venegal
Gütkodierer


 
Join Date: Apr 2009
Posts: 2,127
Thanks: 1
Thanked 426 Times in 424 Posts
venegal has a spectacular aura aboutvenegal has a spectacular aura about
Quote:
Originally Posted by CFEmma View Post
Why should we use this:
Code:
! /\S/.test(node.nodeValue)
Instead of this?
Code:
/\s/.test(node.nodeValue)
Just say what they do in plain english, then it should be pretty obvious:

Code:
/\s/.test(node.nodeValue)
"The node value must contain whitespace."

Code:
! /\S/.test(node.nodeValue)
"The node value must not contain non-whitespace."

So, those are of course two different things. "Containing whitespace" and "containing non-whitespace" are not polar opposites; there are lots of strings that do both. Thus, "containing whitespace" and "not containing non-whitespace" are not the same.

That's the same thing chump2877 said, but I thought it might be easier to understand this way.
venegal is offline   Reply With Quote
Users who have thanked venegal for this post:
CFEmma (02-25-2011)
Old 02-24-2011, 01:58 PM   PM User | #5
Krupski
Regular Coder

 
Krupski's Avatar
 
Join Date: Dec 2010
Location: United States of America
Posts: 502
Thanks: 39
Thanked 47 Times in 46 Posts
Krupski is on a distinguished road
Quote:
Originally Posted by CFEmma View Post
Hello, I have this piece of code I read from a book to remove text nodes that only have white space.

Code:
if (node.nodeType == 3 && ! /\S/.test(node.nodeValue)){
   // code to remove the text node
}
Why should we use this:
Code:
! /\S/.test(node.nodeValue)
Instead of this?
Code:
/\s/.test(node.nodeValue)
Usually, lowercase "special" patterns mean "match this" while uppercase means "match anything BUT this".

For example, \d matches any digit while \D matches anything BUT a digit.

Or said another way, \d is the same as [0-9] and \D is the same as [^0-9]

In "words", \d means "zero through nine" and \D means "not zero through nine".

In your code above, \S (uppercase S) means "match not whitespace" or "match anything that isn't a whitespace".

\s would mean "match a whitespace".

Hope this makes sense.......

-- Roger
__________________
"Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov
Krupski is offline   Reply With Quote
Users who have thanked Krupski for this post:
CFEmma (02-25-2011)
Old 02-25-2011, 05:16 AM   PM User | #6
CFEmma
New to the CF scene

 
Join Date: Aug 2010
Location: Alameda, California, USA
Posts: 6
Thanks: 7
Thanked 0 Times in 0 Posts
CFEmma is an unknown quantity at this point
Thank you everyone! This makes sense!
CFEmma is offline   Reply With Quote
Reply

Bookmarks

Tags
regex, regular expression

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:26 AM.


Advertisement
Log in to turn off these ads.