This article describes the usage of match, charAt, charCodeAt, map, and search in JS common difficult points analysis. Share it for your reference, as follows:
JavaScript match() method
Definition and usage
The match() method can retrieve the specified value within a string, or find a match for one or more regular expressions.
This method is similar to indexOf() and lastIndexOf(), but it returns the specified value instead of the position of the string.
grammar
Match the string and return the specified value
(searchvalue)
Match the regularity and return the specified value
(regexp)
Use match() to retrieve an example string:
<html> <body> <script type="text/javascript"> var str="Hello world!" (("world") + "<br />") (("World") + "<br />") (("worlld") + "<br />") (("world!")) </script> </body> </html>
The final result is, world, null, null, world!
Use match() to retrieve a regular expression match example:
<html> <body> <script type="text/javascript"> var str="1 plus 2 equal 3"; //The regular expression here must be added with g, and the global match will be matched, otherwise a value will be matched and returned((/\d+/g)) </script> </body> </html>
Generally speaking, we use match more often on regularity, and we can also use it to proxy indexOf and lastIndexOf to determine whether this value exists in the string.
JavaScript search() method
Definition and usage
The search() method is used to retrieve the substring specified in the string, or to retrieve the substring matching the regular expression. When the search returns the starting position of the matching substring, the value cannot be retrieved, and returns -1.
grammar
(regexp)
This parameter can be a substring that needs to be retrieved in stringObject or a RegExp object that needs to be retrieved.
To perform a search that ignores case, append the flag i.
Search() Example:
<script type="text/javascript"> var str="Visit W3School!" ((/W3School/)) </script>
Return the index value is 6. Search is usually used in conjunction with regularity to achieve the effect of indexOf.
JavaScript charAt() method
Definition and usage
The charAt() method returns characters at the specified position.
Note that JavaScript does not have a character data type that is different from the string type, so the returned character is a string of length 1.
grammar
Returns the string at the specified location
(index)
chartAt instance:
<script type="text/javascript"> var str="Hello world!" ((1)) </script>
The final result is:e. Usually we can obtain specific characters from a certain string through chartAt.
JavaScript charCodeAt() method
Definition and usage
The charCodeAt() method returns the Unicode encoding of the characters at the specified position. This return value is an integer between 0 - 65535.
The method charCodeAt() is similar to the operation performed by the charAt() method, except that the former returns the encoding of the characters at the specified position, while the latter returns a substring of the character.
grammar
(index)
charCodeAt() instance
Comment: The subscript of the first character in the string is 0. charCodeAt() returns NaN if index is a negative number, or greater than or equal to the length of the string.
<script type="text/javascript"> var str="Hello world!" ((1)) //Return the Unicode encoding 101 of H</script>
js() method
Definition and usage
The map() method returns a new array composed of the return value of each element in the original array called a specified method.
grammar
(callback[, thisArg])
The element in the original array of callback returns a new element after passing through this method.
currentValue, the first parameter of callback, the element currently passed in the array.
index, the second parameter of callback, the index of the currently passed element in the array.
array, the third parameter of callback, an array that calls map method.
ThisArg points to the object this points to when executing the callback function.
The map method calls the callback function once in order for each element in the original array. callback The return values after each execution are combined to form a new array. The callback function is only called on indexes with values; those have never been assigned or used
delete index will not be called. The callback function will be automatically passed in three parameters: array element, element index, and the original array itself.
The first example of using map():
The following code converts all words in an array into the corresponding plural form.
function fuzzyPlural(single) { var result = (/o/g, 'e'); if( single === 'kangaroo'){ result += 'se'; } return result; } var words = ["foot", "goose", "moose", "kangaroo"]; ((fuzzyPlural));
Final results:
["feet", "geese", "meese", "kangareese"]
Find the square root example of each element in the array
var numbers = [1, 4, 9]; var roots = (); /* The value of roots is [1, 2, 3], and the value of numbers is still [1, 4, 9] */
Use map method on strings
var map = var a = ("Hello World", function(x) { return (0); }) // The value of a is [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script type="text/javascript"> //var map = var a = ("Hello World", function(x) { return (0); }) // The value of a is [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] alert(a); </script> </body> </html>
Map compatible with old environments
map is a new method added in the recent ECMA-262 standard; so some older versions of browsers may not implement this method. In browsers that do not have native support for map methods, you can implement it using the following Javascript code. The algorithm used is specified in ECMA-262, 5th edition. Assume that Object, TypeError, and Array have their original values. And the original value is also .
// Implement ECMA-262, Edition 5, 15.4.4.19// Reference: /#x15.4.4.19if (!) { = function(callback, thisArg) { var T, A, k; if (this == null) { throw new TypeError(" this is null or not defined"); } // 1. Assign O as an array that calls map method. var O = Object(this); // 2. Assign len to the length of array O. var len = >>> 0; // 4. If callback is not a function, a TypeError exception is thrown. if ({}.(callback) != "[object Function]") { throw new TypeError(callback + " is not a function"); } // 5. If the parameter thisArg has a value, assign T to thisArg; otherwise T is undefined. if (thisArg) { T = thisArg; } // 6. Create a new array A, with the length of the original array O length len A = new Array(len); // 7. Assign k to 0 k = 0; // 8. When k < len , execute the loop. while(k < len) { var kValue, mappedValue; //Transaction O, k is the original array index if (k in O) { //kValue is the value corresponding to index k. kValue = O[ k ]; // Execute callback, this points to T, and there are three parameters. They are kValue: value, k: index, and O: original array. mappedValue = (T, kValue, k, O); // Add the return value to the new book group A. A[ k ] = mappedValue; } // k increases by 1 k++; } // 9. Return new array A return A; }; }
A clever way to generate timestamps
The first method
function getTimeStamp() { var timestamp=new Date().getTime(); var timestampstring = ();//You must convert the stringoldTimeStamp = timestampstring; return timestampstring; }
The second way
new Date().toString() //It can also achieve the effect, simpler
How to use md5 encryption method:
Reference Google, md5 encrypted library file: /svn/tags/3.1.2/build/rollups/
Actually, it's quite simple. In it, CryptoJS.SHA1() is just referenced to encryption. For example, you can give me a chestnut:
Just call it directly
var keyvaluestring = "ddddd"; sign = CryptoJS.SHA1(keyvaluestring).toString();
PS: Here are a few related encryption and regular tools for your reference:
JavaScript regular expression online testing tool:
http://tools./regex/javascript
Regular expression online generation tool:
http://tools./regex/create_reg
BASE64 encoding and decoding tools:
http://tools./transcoding/base64
URL hexadecimal encryption tool:
http://tools./password/urlencodepwd
MD5 online encryption tool:
http://tools./password/CreateMD5Password
Online hash/hash algorithm encryption tool:
http://tools./password/hash_encrypt
For more information about JavaScript, please view the special topic of this site: "Summary of common JavaScript functions techniques》、《JavaScript object-oriented tutorial》、《Summary of json operation skills in JavaScript》、《Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage》
I hope this article will be helpful to everyone's JavaScript programming.