Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Location
    Alameda, California, USA
    Posts
    6
    Thanks
    7
    Thanked 0 Times in 0 Posts

    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)

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,852
    Thanks
    22
    Thanked 157 Times in 148 Posts
    Because the second line of code will return true even if it contains non-space characters, i.e.:

    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)
    Last edited by chump2877; 02-24-2011 at 06:56 AM.
    Regards, R.J.

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

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • Users who have thanked chump2877 for this post:

    CFEmma (02-25-2011)

  • #3
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts

  • Users who have thanked bullant for this post:

    CFEmma (02-25-2011)

  • #4
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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.

  • Users who have thanked venegal for this post:

    CFEmma (02-25-2011)

  • #5
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    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

  • Users who have thanked Krupski for this post:

    CFEmma (02-25-2011)

  • #6
    New to the CF scene
    Join Date
    Aug 2010
    Location
    Alameda, California, USA
    Posts
    6
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thank you everyone! This makes sense!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •