JS Regular expression rules __ Regular expressions

Source: Internet
Author: User

Methods of regular Expression objects

test, returns a Boolean value that indicates whether the pattern exists in the string being searched. Returns true if present, otherwise returns false.
exec, runs the lookup in the string with the regular expression pattern and returns the package <script type= "Text/javascript src=" http://www.iteye.com/javascripts/tinymce/ Themes/advanced/langs/zh.js "></script><script type=" Text/javascript "src=" http://www.iteye.com/ Javascripts/tinymce/plugins/javaeye/langs/zh.js "></script> An array containing the results of the lookup.
compile, the regular expression is compiled into an internal format, which executes faster.
The properties of the regular Expression object

source, returns a copy of the text of the regular expression pattern. Read-only.
lastindex, returns the character position, which is the starting position of the next successful match in the lookup string.
$1...$9, returns nine of the most recently saved parts found during pattern matching. Read-only.
input ($_), returns a string that performs the lookup of the canonical representation. Read-only.
Lastmatch ($&) that returns the last-matched character in any regular expression search. Read-only.
Lastparen ($+), if any, returns the last child match in any regular expression lookup. Read-only.
leftcontext ($ ') returns the character found between the position from the beginning of the string to the last match in the string being searched. Read-only.
rightcontext ($ ') returns the character from the last match position to the end of the string in the searched string. Read-only.
String objects some methods related to regular expressions

match, finds a match for one or more regular expressions.
Replace, replacing the substring that matches the regular expression.
Search, retrieves a value that matches the regular expression.
split, splits the string into an array of strings.

Character description
/N Line Feed
/R Carriage return character
/T tab
/F page breaks (Tab)
/cx the control character corresponding to X
/b Backspace (BackSpace)
/V Vertical tab
/0 Empty characters ("")

Character class---Simple class, reverse class, Range class, group class, predefined class

The following is a predefined class in a regular expression


Code is equivalent to matching
. ie [^/n], other [^/n/r] matches any character other than line breaks
/d [0-9] matching digits
/d [^0-9] matches non-numeric characters
/s [/n/r/t/f/x0b] matches a white space character
/S [^/n/r/t/f/x0b] matches a non-white-space character
/w [a-za-z0-9_] matches alphanumeric and underline
/w [^a-za-z0-9_] matches a character other than an alpha-numeric underline



Quantifiers (The following quantifier appears as a single greedy quantifier)

Code description
* Match the preceding subexpression 0 or more times. For example, zo* can match "z" and "Zoo". * is equivalent to {0,}.
+ matches the preceding subexpression one or more times. For example, ' zo+ ' can match "Zo" and "Zoo", but cannot match "Z". + is equivalent to {1,}.
? Match the preceding subexpression 0 times or once. For example, "Do (es)" can match "do" in "do" or "does". is equivalent to {0,1}.
{n} n is a non-negative integer. Matches the determined n times. For example, ' o{2} ' cannot match ' o ' in ' Bob ', but can match two o in ' food '.
{N,} n is a non-negative integer. Match at least n times. For example, ' o{2,} ' cannot match ' o ' in ' Bob ' but can match all o in ' Foooood '. ' O{1,} ' is equivalent to ' o+ '. ' O{0,} ' is equivalent to ' o* '.
{n,m} m and n are non-negative integers, where n <= m. Matches n times at least and matches up to M times. Liu, "o{1,3}" will match the first three o in "Fooooood". ' o{0,1} ' is equivalent to ' o '. Notice that there is no space between the comma and the two number.


Greedy quantifiers and inert quantifiers

• Use greedy quantifiers to match, it first will be the whole string as a match, if the match to exit, if it does not match, cut off the last character to match, if not match, continue to the last character truncated to match until there is a match. Until now, the quantifiers we have encountered are greedy quantifiers.
• When using an inert classifier, it first matches the first character as a match, if it succeeds, and if it fails, then tests the first two characters, depending on the number, until a suitable match is encountered.

The inert quantifier simply adds a "?" to the greedy quantifier. Just, like "A +" is greedy match, "A +?" It's inert.

Build a regular expression that verifies the validity of an e-mail address. e-mail address validity requirements (we would like to define): The user name can only contain alphanumeric and underscores, at least one digit, up to 25 digits, followed by the @, followed by the domain name, and domain names require only alphanumeric and minus signs (-), and cannot begin or end with a minus sign. Then followed by the domain name suffix (can have more than one), the domain suffix must be the dot number connected to the 2-4-digit English alphabet

var re =/^/w{1,15} (?: @ (?!) -) (?:(?: [a-z0-9-]*) (?: [A-z0-9] (?! -))(?:/. (?! -)) +[a-z]{2,4}$/;

The basics of a JavaScript regular expression

1 JavaScript regular object creation and usage

declaring JavaScript regular expressions

var recat = new RegExp ("cat");
You can also
var recat =/cat/; Perl Style (recommended)

2 learning 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 Learning under the simple class negative class range class combination class

Simple class
var data = "1LIBINQQ,2LIBINQQ,3LIBINQQ,4LIBINQQ";
var recat =/[123]libinqq/gi;
var arrdata = Data.match (recat);

for (var i = 0; i < arrdata.length; i++)
{
Alert (Arrdata[i]); 1LIBINQQ 2LIBINQQ 3LIBINQQ
}

Negative to Class
var data = "ALIBINQQ,1LIBINQQ,2LIBINQQ,3LIBINQQ,4LIBINQQ"; U0062cf
var recat =/[^a123]libinqq/gi;
var arrdata = Data.match (recat);

for (var i = 0; i < arrdata.length; i++)
{
Alert (Arrdata[i]); 4libinqq
}

Scope class
var data = "Libinqq1,libinqq2,libinqq3,libinqq4,libinqq5"; U0062cf
var recat =/libinqq[2-3]/gi;
var arrdata = Data.match (recat);

for (var i = 0; i < arrdata.length; i++)
{
Alert (Arrdata[i]); LIBINQQ2 libinqq3
}

Combination Class
var data = "a,b,c,w,1,2,3,5"; U0062cf
var recat =/[a-q1-4/n]/gi;
var arrdata = Data.match (recat);

for (var i = 0; i < arrdata.length; i++)
{
Alert (Arrdata[i]); A b C 1 2 3
}

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.