JS Regular Expression Replace substitution variable method _javascript tips

Source: Internet
Author: User

JavaScript is actual combat (will be updated according to the most recent write)

1. JavaScript regular object substitution creation and usage:/pattern/flags First simple case study know what replace can do

Regular expression constructor: New RegExp ("pattern" [, "flags"]);
Regular expression substitution variable function: stringobj.replace (regexp,replace Text);

Parameter description:

Pattern--a regular expression literal
Flags--if present, will be the following values:
G: Global Match
I: Ignore case
GI: Above combination

The following example is used to get the two parameters of the URL and return the real URL before urlrewrite the Var reg=new RegExp (http://www.qidian.com/BookReader/) (\\d+), (\\d+).
aspx "," GMI ");

var url= "http://www.qidian.com/BookReader/1017141,20361055.aspx";
Way one, the simplest common way of Var rep=url.replace (Reg, "$1showbook.aspx?bookid=$2&chapterid=$3");

Alert (rep); Mode two, a callback function with fixed parameters var rep2=url.replace (reg,function (M,P1,P2,P3) {return p1+ "showbook.aspx?bookid=" &
Chapterid= "+p3}";

alert (REP2); Mode three, the callback function with the unfixed parameter var rep3=url.replace (Reg,function () {var args=arguments; return args[1]+ "showbook.aspx?bookid=" +
args[2]+ "&chapterid=" +args[3];


alert (REP3);
Method four//mode four and method three very similar, in addition to returning the replacement string, you can also get the parameter Var BookID;
var Chapterid; 
  function Captext () {var args=arguments;
  BOOKID=ARGS[2];
  CHAPTERID=ARGS[3];
return args[1]+ "showbook.aspx?bookid=" +args[2]+ "&chapterid=" +args[3];
} var rep4=url.replace (reg,captext);
alert (REP4);
alert (BookID);


alert (Chapterid); Use the test method to get the group Var reg3=new RegExp (http://www.qidian.com/BookReader/) (\\d+), (\\d+). ASPX "," GMI ");
Reg3.test ("http://www.qidian.com/BookReader/1017141,20361055.aspx"); 
Gets three group alert (regexp.$1);
alert (regexp.$2); alert (regexp.$3);

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

1 Test checks to see if the specified string exists
var data = "123123″;"
var recat =/123/gi;
Alert (recat.test (data)); True
Check to see if the character is present G continue to go down I case insensitive

2 Exec return query value
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
var recat =/cat/i;
Alert (recat.exec (data)); Cat

3 Match Gets the query array
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
var recat =/cat/gi;
var arrmactches = Data.match (recat)
for (Var i=0;i < arrmactches.length; i++)
{
Alert (Arrmactches[i]); Cat Cat
}

4 Search to return to a location similar to IndexOf
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
var recat =/cat/gi;
Alert (Data.search (Recat)); 23

5 Replace replacement character using regular substitution
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
var recat =/cat/gi;
Alert (Data.replace (Recat, "libinqq"));

6) split using a regular split array
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
var recat =/\,/;
var arrdata = Data.split (recat);
for (var i = 0; i < arrdata.length; i++)
{
Alert (Arrdata[i]);
}
3. Common Expression Collection:

"^\\d+$"//non-negative Integer (positive integer + 0)
"^[0-9]*[1-9][0-9]*$"//Positive integer
"^ ((-\\d+) | (0+)) $ "//non-positive integer (negative integer + 0)
"^-[0-9]*[1-9][0-9]*$"//Negative integer
"^-?\\d+$"//Integer
"^\\d+ (\\.\\d+)? $"//nonnegative floating-point number (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 floating-point numbers
"^ ((-\\d+ (\\.\\d+)?) | (0+ (\\.0+)) $ "//non-positive floating-point number (negative floating-point number + 0)
^ (-([0-9]+\\. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\\. [0-9]+) | ([0-9]*[1-9][0-9]*))] $"
Negative floating-point numbers
"^ (-?\\d+) (\\.\\d+)? $"//floating-point number
"^[a-za-z]+$"//A string of 26 English letters
"^[a-z]+$"//A string of 26 uppercase letters
"^[a-z]+$"///a string consisting of 26 lowercase letters
"^[a-za-z0-9]+$"//A string of numbers and 26 English letters
"^\\w+$"//A string of numbers, 26 English letters, or underscores
"^[\\w-]+ (\\.[ \\w-]+) *@[\\w-]+ (\\.[ \\w-]+) +$ "//email address
"^[a-za-z]+://(\\w+ (-\\w+) *) (\ \\w+ (-\\w+) *)) * (\\?\\s*) $ "//url
"^[a-za-z0-9_]*$".

Basic knowledge of regular expressions

The

^ matches the beginning of an input or line./^a/matches "an A", but does not match "an A"
$ matches an input or a line end,/a$/matches "an A", but does not match "an A"
* matches the preceding metacharacters 0 or more times,/ba*/will match B,ba,ba A,BAAA
+ matches the preceding metacharacters 1 or more times,/ba+/matches ba,baa,baaa
? Matches the preceding metacharacters 0 or 1 times,/ba?/matches the B,ba
(x) match X to save x in the variable named $1...$9
x|y match x or y
{n} exact match n times
{n,} matches n above
{n,m} matching n-m times
[XYZ] Character set (character set), matching any one by one characters (or metacharacters) in this collection
[^xyz] does not match this Any one of the characters in the collection
[\b] matches a backspace
\b The bounds of a word
\b match the non-boundary of a word
\cx here, X is a control character,/\cm/match ctrl-m
\d match a character number character,/\d/ =/[0-9]/
\d matches a non-numeric character,/\d/=/[^0-9]/
\ n matches a newline character
\ r matches a carriage return
\s matches a white space character, including \n,\r,\f,\t,\v, etc.
\s match one A non-white-space character equal to/[^\n\f\r\t\v]/
\ t matches a tab
\v matches a single tab
\w matches a character that can make up a word (alphanumeric, this is my transliteration, with numbers), including underscores, such as [\w] matching 5 in "$5.98" equals [a-za-z0-9]
\w matches a character that cannot be made into words, such as [\w] matches $ in "$5.98", equal to [^a-za-z0-9].

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.