js regex / replace variable replacement method

1, javascript replace regular objects created and Usage: / pattern / flags briefly case studies can replace know what to do next

    Regular expression constructor: new RegExp ( "pattern" [, "flags"]); 

    Regular expression substitution variable functions: stringObj.replace (RegExp, replace Text);

Parameters: 
pattern - a regular expression literal 
flags -, if present, will be the following values: 
G: global matching 
i: ignore case 
gi: more thereof 

// The following examples used to obtain two parameters url and return to the previous real urlRewrite the Url
var
 REG = new new  the RegExp ( "(http://www.qidian.com/BookReader/) (D \\ +), (\ . \ D +) ASPX "," GMI ");
var  URL =" http://www.qidian.com/BookReader/1017141,20361055.aspx ";

// a manner, the easiest way to common
var  REP = URL. Replace (REG, "the bookId 1ShowBook.aspx = $ $ $ = 2. 3 & chapterId?");
Alert (REP);

// Second way, using fixed parameters callback
var  REP2 = url.replace (REG, function (m, P1, P2, P3) { return  P1 + "ShowBook.aspx the bookId =?" + P3 + "& chapterId ="} + P3);
Alert (REP2);

// third approach, the non-fixed parameter callback
var  rep3 url.replace = ( REG, function () { var  args = arguments;  return args[1]+"ShowBook.aspx?bookId="+args[2]+"&chapterId="+args[3];});
alert(rep3);

Method IV //
// four ways and three methods are very similar, in addition to the string returned Alternatively, the parameters may also be obtained separately
var  the bookId;
var  chapterId;
function  capText ()
{
    var  args = arguments; 
    the bookId = args [2] ;
    chapterId = args [. 3];
    return  args [. 1] + "? ShowBook.aspx the bookId =" + args [2] + "& chapterId =" + args [. 3];
}

var  rep4 = url.replace (REG, capText) ;
Alert (rep4);
Alert (the bookId);
Alert (chapterId);

// Get packet using test method
var  REG3 = new new  the RegExp ( "(http://www.qidian.com/BookReader/)(\\d+),(\\d+).aspx", "GMI");
REG3. Test ( "http://www.qidian.com/BookReader/1017141,20361055.aspx");
// Get three packets
Alert (the RegExp $. 1.); 
Alert (the RegExp $ 2.);
Alert (. the RegExp $. 3);

 

2, learn the most commonly used test exec match search replace split 6 Ge methods

1) test to check whether there is a specified character string
var Data = "123123";
var = reCat / 123 / GI;
Alert (reCat.test (Data)); // to true
// Check whether there is character i continue to go down g not case sensitive

2) exec value query return
var = Data "123123,213,12312,312,3, Cat, CAT, dsfsdfs,";
var = reCat / CAT / I;
Alert (reCat.exec (Data)); // Cat

3) match the query array obtained
var = Data "123123,213,12312,312,3, Cat, CAT, dsfsdfs,";
var = reCat / CAT / GI;
var = arrMactches data.match (reCat)
for (I = var 0; I <arrMactches.length; I ++)
{
Alert (arrMactches [I]); // Cat CAT
}

4) search returns the search position similar indexOf
var = Data "123123,213,12312,312,3, Cat, CAT, dsfsdfs,";
var = reCat / CAT / GI;
Alert (data.search (reCat)); / /twenty three

5) replace the replacement character is replaced with n-
var = Data "123123,213,12312,312,3, Cat, CAT, dsfsdfs,";
var = reCat / CAT / GI;
Alert (data.replace (reCat, "libinqq") );

6) split divided using regular array
var = Data "123123,213,12312,312,3, Cat, CAT, dsfsdfs,";
var reCat = / \, /;
var = arrdata data.split (reCat);
for (var 0 = I; I <arrdata.length; I ++)
{
Alert (arrdata [I]);
}

3, commonly used expression collection:

 

"+ $ D ^ \\" // non-negative integer (integer + 0)
"^ [0-9] * [1-9] [0-9] * $" positive integers //
'^ ((- \\ + D) | (0 +)) $ "// non-positive integer (0 + negative integer)
" ^ - [0-9] * [1-9] [0-9] * $ "// negative integer
" ^ - ? \\ d + $ "// integer
" ^ \\ d + (\\. \\ d +)? $ "// non-negative floating-point numbers (positive float + 0)
" ^ (([0-9] + \\ . [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \\ [0-9] +) |. ( [0-9] * [1-9] [0-9] *)) $ "
// positive float
" ^ ((- \\ d + (\\ \\ d +.)) | (0 + (\? ? \ .0 +))) $ "// non-positive float (negative float + 0)
" ^ (- (([0-9] + \\ [0-9] * [1-9] [. 0-9] *) | ([0-9 ] * [1-9] [0-9] * \\ [0-9] +) |. ([0-9] * [1-9] [0 -9] *))) $ "
// negative float
" ^ (-?.? \\ d +) (\\ \\ d +) $ "// float
" ^ [A-Za-z ] + $ " // string consisting of the 26 English letters
"^ [AZ] + $" // string of uppercase letters 26 consisting of
"^ [az] + $" // 26 by the lowercase English letters composition string
""// string of numbers and English letters in 26 ^ [A-Za-z0-9] + $
string" ^ \\ w + $ "// digital, 26 English letters or the underscore
"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"    //email地址
"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$"  //url
"^[A-Za-z0-9_]*$"。

============================================ regular expressions Basics = =============================================

^ Matches the beginning of a line or input, / ^ a / match "an A", without matching "An a" 
$ matches the end of a line or input, / a $ / matching "An a", instead of matching "an A " 
* matches the preceding character zero or more times yuan, / ba * / will match b, BA, BAA, BAAA 
+ matches the preceding character one or more times yuan, / ba + / will match BA, BAA, BAAA 
? match in front meta character 0 or 1, / ba / matching B, BA? 
(x) matches x stored in a variable named $ x $ 9 1 ... 
x | y matches the x or y 
match exactly n times n} { 
{ n,} matches n or more times 
{n, m} matching views nm 
[XYZ] characters (character set), matches any of the set of a character (or character-membered) 
[^ XYZ] does not match this set any character 
[\ b] matches a backspace 
\ b matches a word boundary 
\ B match a word of non-boundary 
\ cX here, X is a control character, / \ cM / matches the Ctrl-M 
\ D matches a word character, / \ D / = / [0-9] / 
\ D matches a non-character words, / \ D / = / [^ 0-9] / 
\ n-match a newline 
\ r matches a carriage return 
\ s matching a blank character, including \ n, \ r, \ f , \ t, \ v like 
\ S matches a non-blank character, equal / [^ \ n \ f \  r \ t \ v] /
\ t matches a tab 
\ v matches a re-direct the tab 
\ character w match that can form words (alphanumeric, this is my translation, including digital), underscore, such as [\ w] matches "$ 5.98" is 5, equal to [a -zA-Z0-9] 
character \ W matches a word can not be composed, such as [\ W] matches "$ 5.98" $ is equal to [^ a-zA-Z0-9]  .

Guess you like

Origin www.cnblogs.com/gavin1024/p/10953280.html