js regular expression (seven)

First, create a regular expression object
method:
in the form of use of the constructor:

    var pattern = new new the RegExp ( 'regular expression', 'modifiers' );
     var pattern = new new the RegExp ( 'hello', 'IgM');   // needs to match the string 'hello'

Method two :
Use of the literal form:

    var pattern = / regular expression / modifiers;
     var pattern = / Hello / IgM;   // needs to match the string 'hello'

Regular expression modifiers:
I (the ignore Case)     does not match case sensitivity [ case insensitive ]
g (global)     throughout the string to find a match [ global ]
m (multiline)       multi-line match lookup [ multi-line ]

Second, the regular expression properties and methods in the prototype object
⑴ prototype property:
RegExp.prototype.global [ pattern.global ]
a Boolean value indicating whether the regular expression modifiers with G

RegExp.prototype.ignoreCase [ pattern.ignore ]
Boolean value indicating whether or not this regular expression modifier with I    

RegExp.prototype.multiline [ pattern.multiline ]
Boolean value indicating whether or not this regular expression modifiers with m    

RegExp.prototype. the lastIndex
If a matching pattern with g, when using the test () and exec () method, this property will be stored at a start location to retrieve the entire string;
If not with g, the lastIndex has been 0

RegExp.prototype.source [pattern.source]
contains the regular expression text    

⑵ prototyping:
Exec () method and test () affect the relationship between the method and the lastIndex:
A, if the regular expression there modifier "g", pattern object maintains lastIndex property, the next recording position began when an exec second, beginning retrieved from the lastIndex.
B, if there is no regular expression modifier "g", does not maintain lastIndex property, each execution start retrieving from a starting position 0

. 1, RegExp.prototype.exec (): [Return Value: the matched result array, or null }
var pattern.exec Result = (STR);


2, RegExp.prototype.test (): [return value: matched returns true; otherwise, returns false }
var = pattern.test pattern ();


. 3, RegExp.prototype. toString ():
the regular expression object converted to the corresponding string of

three, the Javascript string string related methods support for regular expressions
. 1, Search ()
str.search (regex);
a If parameter. is not regular expressions, the first by converting it to RegExp constructor.
b. well support global search returns the position of the first matching substring, if no matching substring, -1.

2, match () [most common regular expression method]
str.match (regex);
A return. Array or a null matching results thereof.


[Regular expression: note whether there is g modified time]
. A g modifier is no, it is not a global match. In this case, the first element of the array will match a string,
the remaining elements are the expression of a positive parenthesized subexpression matches to use .
eg:

    the console.log ( "Hello World HELLO" .match (/ (Hello) / I));   // [ "HELLO", "HELLO"] of the former to match the string, the latter by the regular expression by a circle subexpression parentheses [] with the same pattern string matching

b. When there g modifier, then the method returns an array of packages containing all the matches in the string .
eg:

    "1 plus 2 equals 3".match(/\d+/g) //返回["1","2","3"]


3, replace () { for performing the search and replace operation ]
(regular expression string, to be replaced) str.replace;
EG:

    "helloWorld HEllo".replace(/hello/gi,“JavaScript”); //"JavaScriptWorld JavaScript"


4, split () { string converted into an array of ]
a. A string parameter, it may be a regular expression
b. Segmentation parameter string or a regular expression string form, and rear parts of the divided return an array the elements
eg:

    "1,2,3,4,5" .split ( ","); // [ ". 1", "2", ". 3", ". 4", ". 5"] to, segmentation and returns a string new array 
    ". 1, 2,. 3,. 4,. 5" .split (/ \ * S, \ S * /); // [ ". 1", "2", ". 3", ". 4", ". 5"] separator allows both sides left blank

 

Fourth, the regular expression greedy mode and the non-greedy (lazy mode) [ default greedy mode ]
greedy: when a regular expression of a single module comprising the number of cases: preferred maximum number of matching
non-greedy modes: in a regular when the expression module containing a single frequency: the preferred minimum number of matches
greedy greedy converted to:
the number of times a single regular expression module disposed behind plus ? , Which can be converted
eg:

    var STR = 'Hello World javaScript' ;
     var pattern = / \ W {3,8} / I; 
    the console.log (pattern.exec (STR)); // greedy: results [ 'Hello'] 
    var pattern = ? / \ W {3,8} / I; 
    the console.log (pattern.exec (STR)); // non-greedy: results [ 'hel']

 

Guess you like

Origin www.cnblogs.com/nzcblogs/p/11203286.html