Tuesday, January 15, 2013

JavaScript - The Good Parts - Part 7


JavaScript - The Good Parts - Part 7

This is the last and final post based on the book JavaScript - The Good Parts by Douglas Crockford. In this post, as in the previous post, we will continue with some more methods in the pre-defined objects of JavaScript.

RegExp
regexp.exec(string) The exec is the most powerful (and the slowest) of all methods that use regular expression. If it successfully matches the regexp and the string, it returns an array. The “0” element of the array will contain the substring that matched the regexp. The 1 element is the text captured by the group 1, the 2 element is the text captured by group 2, and so on. If the match fails it will return a null.
If the regexp has a “g” flag, things are a little complicated. The searching begins not at the position 0, but at the position regexp.lastIndex (which is initially zero). If the match is successful then the regexp.lastIndex will be set to the position of the first character after the match. An unsuccessful match resets the regexp.lastIndex to 0.

regexp.test(string) This is the simplest and the fastest of the methods that use regular expression. If the regexp matches the string it returns true, otherwise it returns false. Do not use “g” flat with this method.
var b = /&.+;/.test('frank & beans'); //b is true.

String
string.charAt(pos) This returns the character at the position “pos” in this string. If pos is less than zero or greater than or equal to the length of the string, it returns an empty string.
var name = 'Curly';
var initial = name.chartAt(0); //initial is "C"

string.charCodeAt(pos) This returns an integer representation of the character at position “pos” instead of the character. If pos is less than zero or greater than or equal to the length of the string, it returns a NaN.
var name = "Curly";
var initial = name.chartCodeAt(0); //initial is “67”

string.concat(string …) This method makes a new string by concatenating the other strings together.
var s = 'C'.concat('a', 't'); //value of c is "Cat"

string.indexOf(searchString, position) This searches for the "searchString" from "position" in the string. This returns a -1 if this string is not found, else it returns the index at which the searchString is found.
var text = “Mississippi”;
var p = text.indexOf(‘ss’); //p is 2
p = text.indexOf(‘ss’, 3); //p is 5
p = text.indexOf(‘ss’, 6); //p is -1

string.lastIndexOf(searchString, position) This searches for the "searchString" from "position" in the string but in the reverse direction. This returns a -1 if this string is not found, else it returns the index at which the searchString is found.
var text = "Mississippi";
var p = text.lastIndexOf('ss'); //p is 5
p = text.lastIndexOf('ss', 3); //p is 2
p = text.indexOf('ss', 6); //p is 5

string.localeCompare(that) This compares two string and returns a -1 if the "this" string is smaller than the second, 0 if they are equal and 1 if the "this" string is greater than the second.
var m = ['AAA', 'A', 'aa', 'a', 'Aa', 'aaa'];
m.sort(function (a, b) {
  return a.compareLocale(b);
});

//m in some locale after sorting is ['a', 'A', 'aa', 'Aa', 'aaa',  'AAA'];

string.match(regexp) This matches a string and a regular expression. How it does depends on the "g" flag. If no "g" flag is specified then the result of calling string.match(regexp) is same as calling regexp.exec(string). However if the regexp has the "g" flag, then it produces an array of all the matches, but excludes capturing groups.

string.replace(searchValue, replaceValue) The replace method searches the string for occurrence of "searchValue" and replaces them with "replaceValue". The "searchValue" can be a regular expression object. If it is a string then only the first occurrence of searchValue is replaced.
If the searchValue is a regular expression and it has the "g" flag then all the occurrences will be replaced. If it does not have a "g" flag then only the first occurrence will be replaced.
The replaceValue is a string then the character $ has a special meaning.
//Capture 3 digits within parenthesis
var oldareacode = /\((\d{3})\)/g;
var p = ‘(555)666-1212’.replace(oldareacode, ‘$1-‘); //p is 555-666-1212

Dollar Sequence Replacement
$$ $
$& The matched text
$number Capture group text
$` The text preceding the match
$’ The text following the match
If the replaceValue is a function, it will be called for each match, and the string returned by the function will be used as the replacement text. The first parameter passed to the function is the matched text. The second parameter is the text of the capture group 1, the next parameter is of group 2 and so on.

string.search (regexp) This is like the indexOf method except that it takes a regexp. If "g" parameter, if passed is ignored.

string.slice(start, end) This is like the array slice. It returns the string value from the start to the end -1 position. If the start parameter is negative, it adds string.length to it.  If no end parameter is specified then it is taken to be string.length. If the end parameter is negative it adds the string.length to it.
var text = 'and in it he says "Any damn fool could';
var a = text.slice(18); //a is '"Any damn fool could'
var b = text.slice(0, 3); //b is 'Any'
var c = text.splice(-5); //c is 'could'
var d = text.slice(19,32); //d is 'Any damn fool'
var e = text.slice(19, -5); //d is 'Any damn fool '

string.split(separator, limit) The split method creates an array of strings by splitting this string into pieces. The optional limit parameter can limit the number of pieces that will be split. The separator parameter can be a string or a regular expression.
If the separator is the empty string, an array of single characters is produced.
var digits = '0123456789';
var a = digits.split('', 5); //a is ['0', '1', '2', '3', '456789'];
Otherwise the string is searched for all occurrences of the separator. Each unit of text between separators is copied into the array. The "g" flag is ignored.
var ip = "192.168.32.45";
var b = ip.split('.'); //b is ['192', '168', '32', '45'];
var c = '|a|b|c|'.split('|'); //c is ['', 'a', 'b'. 'c', ''];

string.substring(start, end) This is same as the slice method except it does not adjust for negative values.

string.toLocaleLowerCase() Converts the string to a lower case based on the locale.

string.toLocaleUpperCase() Converts the string to an upper case based on the locale.

string.toLowerCase() Same as toLocaleLowerCase() but without considering the locale.

string.toUpperCase() Same as toLocaleUpperCase() but without considering the locale.

string.fromCharCode(char …) The converts a series of numbers to a string.
var a = String.fromCharCode(67, 97, 116); // a is 'Cat'

Conclusion JavaScript is a very powerful language and it has its good parts. It also has its dark side, like the global variables, but if one takes the trouble to learn and understand the language then one can work wonders with this language.

To end the series here is a series of blogposts by Venkat Subramanian which will serve as a very good introduction to functional programming for the uninitiated.
http://blog.agiledeveloper.com/2012/11/functional-style-of-programming-in_14.html
http://blog.agiledeveloper.com/2012/11/functional-style-of-programming-in_15.html
http://blog.agiledeveloper.com/2012/11/functional-style-of-programming-in_16.html
http://blog.agiledeveloper.com/2012/11/functional-style-of-programming-in.html

No comments: