JavaScript Match () method
Definition and usage
The match () method retrieves the specified value within a string, or finds a match for one or more regular expressions.
The method is similar to IndexOf () and lastIndexOf (), but it returns the specified value instead of the position of the string.
- Grammar
- Matches a string, returning the specified value
- Stringobject.match (Searchvalue)
- Matches the regular, returning the specified value
- Stringobject.match (RegExp)
Use Match () to retrieve a string example:
-
- <body>
- <script type= "Text/javascript" >
- var str= "Hello world!"
- document.write (Str.match ("World") + "<br/>")
- document.write (Str.match ("World") + "<br/>")
- document.write (Str.match ("worlld") + "<br/>")
- document.write (Str.match ("world!"))
- </script>
- </body>
-
The final result is, world,null,null,world!
Use Match () to retrieve a matching example of a regular expression:
-
- <body>
- <script type= "Text/javascript" >
- var str= "1 plus 2 equal 3";
- The regular expression here must be preceded by a G, global match, or it will match a value that returns
- document.write (Str.match (/\d+/g))
- </script>
- </body>
-
In general, we use match for more than regular, and we can use it to proxy indexof and LastIndexOf to determine if this value exists in the string.
JavaScript Search () method
Definition and usage
The search () method retrieves the substring specified in the string, or retrieves a substring that matches the regular expression, retrieves the starting position of the matched substring, fails to retrieve the value, and returns-1.
- Grammar
- Stringobject.search (RegExp)
- This parameter can be a substring that needs to be retrieved in Stringobject, or it can be a RegExp object that needs to be retrieved.
- To perform a search that ignores the case, append the flag I.
Copy Code
Search () Example:
- <script type= "Text/javascript" >
- var str= "Visit w3school!"
- document.write (Str.search (/w3school/))
- </script>
The return index value of 6,search is usually used with regular mates to achieve the indexof effect.
JavaScript charAt () method
Definition and usage
The CharAt () method returns the character at the specified position.
Note that JavaScript does not have a character data type that differs from the string type, so the returned character is a string of length 1.
- Grammar
- Returns a string at the specified position
- Stringobject.charat (Index)
Chartat instances:
- <script type= "Text/javascript" >
- var str= "Hello world!"
- document.write (Str.charat (1))
- </script>
The final result is: E, usually we can get specific characters from a string by Chartat.
JavaScript charCodeAt () method
Definition and usage
The charCodeAt () method returns the Unicode encoding of the character at the specified position. This return value is an integer between 0-65535.
Method charCodeAt () is similar to the operation performed by the CharAt () method, except that the former returns the encoding of the character at the specified position, and the latter returns a character string.
- Grammar
- Stringobject.charcodeat (Index)
charCodeAt () instance
Note: The subscript for the first character in a string is 0. If index is a negative number, or is greater than or equal to the length of the string, charCodeAt () returns NaN.
- <script type= "Text/javascript" >
- var str= "Hello world!"
- document.write (Str.charcodeat (1))
- Returns the Unicode encoding of H 101
- </script>
JS in Array.prototype.map () method
Definition and usage
The map () method returns a new array that consists of a return value from each element in the original array that invokes a specified method.
- Grammar
- Array.map (callback[, Thisarg])
- Callback elements in the original array return a new element after the method.
- The first argument of Currentvalue,callback, the element that is currently passed in the array.
- The second argument of Index,callback, the index of the element that is currently being passed in the array.
- The third argument of Array,callback, which calls an array of map methods.
- Thisarg executes the callback function when this object is pointed to.
The map method invokes the callback function sequentially for each element in the original array. Callback the return value after each execution is combined to form a new array. The callback function will only be called on the indexed index;Those who have never been assigned value or used
Delete-deleted indexes are not called。 The callback function is automatically passed in three parameters: an array element, an element index, and the original array itself.
The first example of using a map ():
The following code converts all the words in an array to the corresponding plural form.
- function Fuzzyplural (single) {
- var result = Single.replace (/o/g, ' e ');
- if (single = = ' Kangaroo ') {
- result + = ' se ';
- }
- return result;
- }
- var words = ["foot", "Goose", "Moose", "kangaroo"];
- Console.log (Words.map (fuzzyplural));
- Final results ["feet", "geese", "Meese", "Kangareese"]
An example of the square root of each element in an array
- var numbers = [1, 4, 9];
- var roots = Numbers.map (MATH.SQRT);
- /* The value of roots is [1, 2, 3], the value of numbers is still [1, 4, 9] */
Using the Map method on a string
- var map = Array.prototype.map
- var a = Map.call ("Hello World", function (x) {return x.charcodeat (0);})
- The values for a are [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
- <! DOCTYPE html>
-
-
- <meta charset= "UTF-8" >
- <title></title>
-
- <body>
- <script type= "Text/javascript" >
- var map = Array.prototype.map
- var a = Array.prototype.map.call ("Hello World", function (x) {return x.charcodeat (0);})
- The values for a are [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]
- alert (a);
- </script>
- </body>
-
Map compatible with legacy environments
Map is a newly added method in the recent ECMA-262 standard, so some older browsers might not have implemented the method. In browsers that do not natively support the map method, you can use the following Javascript code to implement it. is used by
The algorithm is ECMA-262, the 5th edition stipulates. Assume that the object, TypeError, and Array have their original values. And the original value of Callback.call is also Function.prototype.call.
- Implement ECMA-262, Edition 5, 15.4.4.19
- Reference: http://es5.github.com/#x15.4.4.19
- if (! Array.prototype.map) {
- Array.prototype.map = function (callback, THISARG) {
- var T, A, K;
- if (this = = null) {
- throw new TypeError ("This is a null or not defined");
- }
- 1. Assign o to the array that calls the map method.
- var O = Object (this);
- 2. Assign Len to the length of the array O.
- var len = o.length >>> 0;
- 4. If callback is not a function, the TypeError exception is thrown.
- if ({}.tostring.call (callback)! = "[Object Function]") {
- throw new TypeError (callback + "is not a function");
- }
- 5. If the parameter thisarg has a value, the T is assigned the value Thisarg; otherwise t is undefined.
- if (Thisarg) {
- T = Thisarg;
- }
- 6. Create new array A, length is the original array o length len
- A = new Array (len);
- 7. Assign a value of K to 0
- k = 0;
- 8. When K < Len executes the loop.
- while (K < Len) {
- var kvalue, Mappedvalue;
- Traverse O,k as the original array index
- if (k in O) {
- Kvalue the value corresponding to the index K.
- Kvalue = o[K];
- The execution callback,this points to T, with three parameters. Kvalue: value, K: Index, O: the original array.
- Mappedvalue = Callback.call (T, Kvalue, K, O);
- The return value is added to the new book Group A.
- a[K] = Mappedvalue;
- }
- K Self-Increment 1
- k++;
- }
- 9. Returns the new array a
- return A;
- };
- }
Common several JS difficult points, Match,charat,charcodeat,map,search