1

I was just going through the code of addClass function of jQuery library as i need to make a function similar to this for my own library and i came across the following line of code:

cur = elem.nodeType === 1 && ( elem.className ?
                    ( " " + elem.className + " " ).replace( rclass, " " ) :
                    " "
                ); 

This i believe returns the class of current node, i understand most part of it , until i met my menace, regex's , rclass , now rclass is actually /[\t\r\n\f]/g , now if i use a ONLINE TOOL to decode this regrex, i get the following:

/[\t\r\n\f]/g
[\t\r\n\f] match a single character present in the list below
\t Tab (ASCII 9)
\r matches a carriage return (ASCII 13)
\n matches a line-feed (newline) character (ASCII 10)
\f matches a form-feed character (ASCII 12)
g modifier: global. All matches (don't return on first match)

I am not quite able to make perfect sense of the above what does the above really mean , ok i understand \t Tab , what on earth is line-feed and form-feed ?

THIS QUESTION neither has a accpted answer nor is it convincingly answered by any of the answer's

Thank you.

Alex-z.

Community
  • 1
  • 1
Alexander Solonik
  • 9,838
  • 18
  • 76
  • 174
  • 2
    possible duplicate of [What are carriage return, linefeed, and form feed?](http://stackoverflow.com/questions/3091524/what-are-carriage-return-linefeed-and-form-feed) – CBroe Sep 27 '15 at 21:33
  • Basically the expression is looking for different kinds of white space characters, that can be used to separate multiple classes in the `class` attribute/`className` property. – CBroe Sep 27 '15 at 21:35

3 Answers3

1

They are essentially just other types of line-breaking whitespace characters.

The call to replace() is replacing all instances of tabs and line-breaks in the class name with a simple empty string (" ").

If you really want to know more, you can read a lot about new lines.

duncanhall
  • 11,035
  • 5
  • 54
  • 86
1

They are whitespace characters. \n or line-feed is the equivalent to Enter or Return (\r and \n are really similar, with some oddities in linux/osx/windows). \f I don't know how it's used, but from what I found is also a form of whitespace.


As a matter of fact, I did a small library to replicate some of jQuery functionality. It's unfinished and unmaintained, but that part was indeed finished. I implemented it like this:

/**
 * .addClass(name)
 * 
 * Add a class to the matched nodes
 * Possible polyfill: https://github.com/eligrey/classList.js
 * @param String name the class name we want to add
 * @return this Umbrella object
 */
u.prototype.addClass = function(name){
  // Loop through all the nodes
  this.each(function(){
    // Allow for several class names like "a b c"
    this.classList.add(name.split(" "));
  });
  return this;
};
Community
  • 1
  • 1
Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90
1

Form feed (\f) is basically a page break character. It isn't used much these days but is still used in some editors.

Carriage return (\r) and line feed (\l) are the new line characters. These are common place and are usually donated by new line (\n), which is way of saying 'whatever the new line character(s) are on the current system'.

In UNIX related systems (eg. Linux and OSX) then only \l is used where-as Windows uses \r\l. Old Mac's (pre OSX) use \r.

All these are old typewriting terms that have been carried over to computers. Windows has copied this more precisely by donating new line as both go down and across the page.

Stephen Simpson
  • 1,381
  • 1
  • 11
  • 23