Basic knowledge of JavaScript regular expressions

Source: Internet
Author: User
Tags lowercase regular expression

Basic knowledge of JavaScript regular expressions

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-4n]/gi;
var arrdata = Data.match (recat);

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


JavaScript is used when validating forms

"^-[0-9]*[1-9][0-9]*$"//Negative integer

"^-?d+$"//Integer

"^d+ (. d+)? $"//nonnegative floating-point number (positive 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]*)] $ "//Positive floating-point number

^ ((-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 number

"^ (-?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


function Istruename (s)
{
var patrn=/^[a-za-z]{1,30}$/;
if (!patrn.exec (s)) return false
return True
}
}}

Verify password: Can only enter 6-20 letters, numbers, underscores
<pre Class=java name= "code" >function ispasswd (s)
{
var patrn=/^ (W) {6,20}$/;
if (!patrn.exec (s)) return false
return True
}
</PRE>
<BR>
<br>//Check Ordinary telephone, fax number: You can "+" start, in addition to numbers, can contain "-"
<br><pre Class=java name= "code" >function Istel (s)
{
var patrn=/^[+]{0,1} (d) {1,3}[]? ([-]? (d) {1,12}) +$/;
var patrn=/^[+]{0,1} (d) {1,3}[]? ([-]? ((d) | []) {1,12}) +$/;
if (!patrn.exec (s)) return false
return True
}
</PRE>
<BR>
<br>//Check mobile phone number: Must start with a number, in addition to the number, can contain "-"
<br><pre Class=java name= "code" >function Ismobil (s)
{
var patrn=/^[+]{0,1} (d) {1,3}[]? ([-]? ((d) | []) {1,12}) +$/;
if (!patrn.exec (s)) return false
return True
}
</PRE>
<BR>
<br>//Check ZIP code
<br><pre Class=java name= "code" >function Ispostalcode (s)
{
var patrn=/^[a-za-z0-9]{3,12}$/;
var patrn=/^[a-za-z0-9]{3,12}$/;
if (!patrn.exec (s)) return false
return True
}
</PRE>
<BR>
<br>//Check Search Keywords
<br><pre Class=java name= "code" >function Issearch (s)
{
var patrn=/^[^ ' ~!@#$%^&* () +=|\][]{}:; ',. <>/?] {1} [^ ' ~!@$%^& () +=|\]
[]{}:; ',. <>?] {0,19}$/;
if (!patrn.exec (s)) return false
return True
}

function IsIP (s)//by zergling
{
var patrn=/^[0-9.] {1,20}$/;
if (!patrn.exec (s)) return false
return True
}
</PRE>
<BR>
<br><span style= "font-size:18pt" > Regular expression </SPAN>
<br><pre Class=java name= "code" > "^\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_]*$"
</PRE>

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.