JavaScript regular expressions, jquery regular Expressions __ Regular expressions

Source: Internet
Author: User

http://zhangyanyansy.blog.163.com/blog/static/135305097201042110756638/

Basic knowledge of JavaScript regular expressions

1 JavaScript regular object creation and usage declaration 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)); Tr

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"; \ 拏

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″;" \ 拏

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″;" \ 拏

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

}

These are JS is the most basic method of use.

Second, the jqry regular expression processing

1.test method

reg=/^\w{3,}@\w+ (\.\w+) +$/;
2 if (!reg.test (obj)) {
3 $ ("#test"). HTML ("<b> Please enter the correct email address </b>");
4 }
2.exec method
$.formatdate.patternparts =/^ (yy (yy)? | M (m (m)?)? | D (d)? | EEE (E)? | a| H (h)? | H (h)? | M (m)? | s (s)? | S)/;
2 var matched = $.formatdate.patternparts.exec (pattern);
3 if (matched) {
4 Null if conforming to matched array index:0
5 Result.push (this,date) ($.formatdate.patternval[matched[0]].call);
6 Pattern = Pattern.slice (matched[0].length);
7 }
Related Article

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.