Regular: varsubStrstrreplace (regstr, & 039; & 039;); returns the replaced character seek without changing the original string; if you do not need regular expressions, you can only Replace the first matched character; varsubStrstrmatch (reg); returns the matched string.
Regular Expression:
Var subStr = str. replace (reg/str, ''); returns the replaced character seek without changing the original string; if regular expressions are not used, only the first matched character can be replaced;
Var subStr = str. match (reg); returns the matched string. If no global match is specified, only match the first character, which is generally used with the regular expression;
Var bool = reg. test (str) ;==>Check whether the regular expression matches the Regular Expression in str.,
Var Int= Str. search (reg/str );Returns the first occurrence location of the reg/str to be searched.,No response found-1;Search for small strings in large strings;It can be used with the match, but global match does not work, because only the position of the first matched character is returned;
Var subStr = str. charAt (int)Returns a character at the specified position;
Var subStr = str. sbustring (start, [end]);Does not include the end bit;
Var subStr = str. substr (start, length );
Var arr = str. split ('delimiter'); The default value is comma;
Regular-compatibleStringFunction;
* *** The most important use of regular-compatible str functions and regular expressions is that Fuzzy Matching and global matching can be used. If you only search for a single string, you do not need to use regular expressions;
1 str. split (preg) ==> separates str into an Array Based on the Content matched by a regular expression. Note that if a subexpression exists, in addition to the overall regular expression, it is also separated by a subexpression, when using the split function, it is best not to use a regular expression;
Test code:
Var str = 'aabbccddaabbccdd ';
Var preg =/aa/g;
Var res = str. split (preg );
// Alert (res. constructor); // function Array;
2Str. Match (preg) =>Returns an array.;
Test code:
Var str = 'aabbccddaabbccdd ';
Var preg =/aa/g;
Var a = str. match (preg );
// Alert (a. constructor); // function Array;
// Alert (a. length); // 2;
// Alert (a); // aa, aa
3 str. search
Var str = 'aabbccddaabbccdd ';
Var preg =/aa/g;
Var res = str. search (preg );
// Alert (res. constructor); // function Number; global match does not work;
// Alert (res); // 0: The first matched position is not found.-1 is returned;
4str. replace;
Var str = 'aabbccddaabbccdd ';
Var preg =/aa/g;
Var res = str. replace (preg ,'*');
Alert (res); // * bbccdd;
// Search for aabbcc and replace bb *
Var res1 = str. replace (preg, '$1*$2 ');
Alert (res1); // aa * ccddaa * ccdd;
Regular Functions;
1 test;
VarBool = preg. test (str );
2 exec
Preg.exe c (str); // jsThe most powerful regular functions;
Demo:
Var str = 'aabbccddaabbccdd ';
Var preg =/aa (bb)/g;
Var reselect preg.exe c (str );
// Alert (res); // aabb, aa; // The returned result is an array. The res [0] of this array is the whole of the regular match, followed by a matched subexpression. When fuzzy match is performed on a string, the returned results can be used to determine the exact match;
// In js, everything is an object. The returned array contains two attributes: index and input;
// Alert (res. index); // 0 returns the offset;
// Alert (res. input); // return str for aabbccddaabbccdd;
// Var reselect preg.exe c (str );
// Var res1‑preg.exe c (str );
// Alert (res. index); // 0 // This is similar to the function mysql_fetch_assoc (res) used to obtain mysql resources in php. You can use a while statement to print all globally matched information;
// Alert (res1.index); // 8
Finally, let's try again.;Retrieve all information;
Var str = 'aabbccddaabbccdd ';
Var preg =/aa (bb)/g;
While(res=preg.exe c (str )){
For (key in res ){
Document. write (key + '=' + res [key] +'
');
}
}
0 = aabb
1 = bb
Index = 0
Input = aabbccddaabbccdd
0 = aabb
1 = bb
Index = 8
Input = aabbccddaabbccdd
Case-sensitive Regular Expressions;
I. case insensitive;
Js regular object RegExp,Includes some attributes,Such as the matching mode I m g s;
Demo var reg =/^ \S+ | \ S + $/Img; reg. source = return^ \S+ | \ S + $
Reg. Global =>Returns true.==> (Because the created reg object specifies global matching)
/^ \S+ | \ S + $/g ==>Space at the beginning and end of the row;
/^ \ S $/mg; ==> matches empty rows;
| Or note:/a | B | c/=/[abc]/;
The first usage of square brackets [] is the first usage ==> or
The second use [a-z] range => indicates one in the azimuth;
Method 3 ^ put in square brackets [^] to indicate inverse;
[\ U4e00-\ u9fa5] ==> match Chinese characters;
To match a single Chinese character, you can check the Chinese unicode hexadecimal encoding;
^Beginning of Line
$ End of line
\ B: the boundary character of the word must start or end with a space or a comma;
\ B word non-Boundary
[^]Except XXX;
.Represents any character
+Representative{1,N}Same as the preceding characters.
\ DDigital
\ D [^ 0-9] is opposite to \ d
\W[0-9a-zA-Z _] word
\W [^ 0-9a-zA-Z _] Note/[a-zA-Z _]/==/ [a-z]/ig;
\ S space
\S Characters except spaces;
\ SS matches all non-null characters
{N, m}
{N ,}
{, M}
{N}
{1 ,}=> +
{0 ,}=> *
{0, 1 }=>Appears0Time or once
Multiline Mode M(Each line of a large string is treated as a start and end. If you do not use the multiline mode, no matter how many lines there are, the positive string has only one start and one end.);
Question:
Replace the character at the end of each line #;
Single Row Mode(In php, if the regular expression match mode is set to single-line mode, that is, the (point). metacharacters represent any character, including line breaks;)--Purpose,If a line break exists between the characters to be matched,The dot metacharacters cannot be matched, resulting in no matching results.
In js, the single-line mode is not supported. You can use a pair of negative characters to replace the single-line mode;
Pre-query (assertion) ==> pre-query characters are not included in the results,
Backward pre-query (assertion) (pre-judge whether the previous value is a set value) syntax==>(? = Xxx );
Or (pre-determine whether the previous value is not a specific value) syntax => (?! Xxx );
1 // search for the root of a word ending with ing/\ B \ w +? (? = Ing \ B)/ig; // the word itself contains ing, but the returned result is the part after ing;
Var preg =/\ bwin (? = Xp \ B) xp \ B /;//To include the preference Section,Add the pre-check content after the pre-check;
2 // search for words starting with win and not win95;
Var str = 'winxp winstart win95 win2003 ';
Var preg =/\ bwin (?! 95) \ w +? \ B/ig;
Var str1 = str. match (preg );
Alert (str1); // winxp winstart win2003
3 // find the root of the word starting with "un":
VarStr = 'Happy unknown happy undo ';
Var preg =/\ B (? = Un) \ w +? \ B/gi;
Var str1 = str. match (preg );
Alert (str1); // happy known do
Forward pre-check (assertion): (js is not supported currently) php (supported );
Reverse reference subexpression:
(? : Pattern) matches pattern but does not get the matching result. That is to say, this is a non-get match and is not stored for future use;
Use \ 1, \ 2 \ 3... in the regular expression ........ \ N reference;
$1, $2 ,..... $ N reference;
Perfect getByClass
Function getByClass (oParent,ClassName){
Var aEle = oParent. getElementsByTgeName ('*');
VarAResult = [];
Var reg =/\ B + className \ B +/
Var I = 0;
For (I; I
If (reg. test (aEle [I]. className )){
AResult. push (aEle [I]);
}
}
Return aResult;
}
PhpRegular;The regular expression syntax of php is almost the same as that of js, but the regular expression functions are different.
// Php regular functions; Replace the array with regular expressions; 1: array preg_grep (string $ pattern, array $ input [, int $ flags = 0]); The third optional parameter $ flag has only one value, PREG_GREP_INVERT. If this parameter is specified, no matching array elements are returned (I .e., inverse );
Regular Expression matching; 2 int preg_match (string $ pattern, string $ subject [, array & $ matches]) The third optional parameter stores the matched string (one-dimensional array). It is best not to use the next two parameters. If 4th optional parameters are used, the number of digits of the array increases; Return value. If the matching succeeds, 1 is returned. If no matching result is returned, 0 is returned;
3int preg_match_all (string $ pattern, string $ subject [, array & $ matches]);
Parameters and usage are the same as preg_match; Return value, which matches the number of times matched; if no match is returned, 0 is returned;
4. Regular Expression replacement; Mixed preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1) This function is replaced globally by default (-1). If you do not want to replace all functions, you can specify the number of replacement times for the fourth optional parameter;
5. Regular Segmentation Array preg_split (string $ pattern, string $ subject [, int $ limit =-1]); This function returns an array. The fourth optional parameter specifies the maximum length of the array to be returned (this parameter can be used to segment the string randomly );
6. Escape special characters; String preg_quote (string $ str [, string $ delimiter = NULL]); This function adds a backslash to the special characters passed in the $ strcan parameter that have special meanings in the real world. The second optional parameter can be used to specify any character to be escaped; this function can be used with the previous regular functions to exert greater power; Demo1: $ Keywords = '$40 for a g3/100 '; $ Keywords = preg_quote ($ keywords ,'/'); Echo $ keywords; // returns \40 40 for a g3 \/400
Demo2: $ Textbody = "This book is * very * difficult to find ."; $ Word = "* very *"; $ Textbody = preg_replace ("/". preg_quote ($ word )."/", "". $ Word ."", $ Textbody ); |