Quote:
Originally Posted by CFEmma
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