The syntax and examples of common regular expressions in jquery

Source: Internet
Author: User

quickexpr =/^ (?: [^<]* (<[ww]+>) [^>]*$|# ([w-]+) $)/
(?:...) Represents a non-capture type

[^<] indicates that it starts with "<" and contains 0 or more ' < ' brackets

(<[ww]+>) is a catch type that starts with ' <> ' and contains one or more characters in the middle

$ indicates the end of a character

(# ([w-]+)) means a catch type, with ' # ' numbers and strings, numbers, _, and-

Rnotwhite =/s/
s represents a symbol other than a blank character

Trimleft =/^s+/trimright =/s+$/www.111cn.net
The left and right edges of the blank. S is a blank character. ^ prefix denotes string start, $ suffix indicates end of string

Rdigit =/d/
Indicates a number

Rsingletag =/^< (w+) s*/?> (?:</1>)? $/
^< (w+) s*/?>
The expression starts with ' < ', contains one to many characters, and 0 to multiple blanks, 0 or one '/' and ' > ' End,

(?:</1>)? $


See some common jquery regular expression instances

<script type= "text/web Effects" language= "JavaScript" >
function Validata () {
if ($ ("#username"). val () = "") {
document.write ("Please enter first name");
return false;
}
if ($ ("#password"). val () = "") {
document.write ("Please enter the password");
return false;
}
if ($ ("#telephone"). val () = "") {
document.write ("Please enter the telephone number");
}
if ($ ("#email"). val () = "") {
$ ("#email"). Val ("shuangping@163.com");
}
}

function Isinteger (obj) {

reg=/^[-+]?d+$/;
if (!reg.test (obj)) {
$ ("#test"). HTML ("<b>please input correct figures</b>");
}else{
$ ("#test"). HTML ("");
}
}
function Isemail (obj) {
reg=/^w{3,}@w+ (. w+) +$/;
if (!reg.test (obj)) {
$ ("#test"). HTML ("<b> Please enter the correct email address </b>");
}else{
$ ("#test"). HTML ("");
}
}
function isstring (obj) {
reg=/^[a-z,a-z]+$/;
if (!reg.test (obj)) {
$ ("#test"). HTML ("<b> can only enter characters </b>");
}else{
$ ("#test"). HTML ("");
}
}
function Istelephone (obj) {
reg=/^ (d{3,4}-)? [1-9]d{6,7}$/;
if (!reg.test (obj)) {
$ ("#test"). HTML ("<b> Please enter the correct phone number!") </b> ");
}else{
$ ("#test"). HTML ("");
}
}
function IsMobile (obj) {
reg=/^ (+d{2,3}-) d{11}$/;
if (!reg.test (obj)) {
$ ("#test"). HTML ("Please enter the correct mobile phone");
}else{
$ ("#test"). HTML ("");
}
}
function Isuri (obj) {
reg=/^http://[a-za-z0-9]+. [a-za-z0-9]+[/=?%-&_~ ' @[] ': +!] * ([^<> ""]) *$/;
if (!reg.test (obj)) {
$ ("#test"). HTML ($ ("#uri"). Val () + "Please enter the correct inernet address");
}else{
$ ("#test"). HTML ("");
}
}

Document loading complete execution
$ (document). Ready (function () {
Do something here

Interlace color function
$ ("P"). each (function (i) {
this.style.color=[' red ', ' green ', ' blue ', ' black '][i%2]
});

EQ (2) gets the 3rd element of the $ ("P") collection
$ ("P"). EQ (2). Click (function () {$ ("#display"). CSS Tutorial ("Color", "Blue")};

All p in test has the style "over" attached.
$ ("#test >p"). AddClass ("over");

The last p in test attaches the style "out".
$ ("#test p:last"). AddClass ("Out");

Select sibling element Not yet understood
$ (' #faq '). Find (' dd '). Hide (). End (). Find (' DT '). Click (Function ()

Select parent Element
$ ("a"). Hover (
Function () {$ (this). Parents ("P"). AddClass ("Out")},
Function () {$ (this). Parents ("P"). Removeclass ("Out")})


Hover mouse hover effect, toggle switch the function you want to call every time you click,
Trigger (EventType): Triggers a class of events on each matching element.
Bind (EVENTTYPE,FN), Unbind (EventType): The binding of an event is bound to delete a binding event from each matching element (added).

Method of ligatures
$ ("#display"). Hover (function () {
$ (this). addclass ("over");
},function () {
$ (this). Removeclass ("over");
})
. Click (function () {Alert ($ ("#display"). Text ())};




if ($.browser.msie) {//Judging browser, if IE performs the following functions

Focus
$ ("input[@type =text],textarea,input[@type =password]")
. focus (function () {$ (this). CSS ({background: "White", border: "1px solid Blue"})}
It can be written in this way,
. blur (Function () {$ (this). CSS ({background: ' White ', border: ' 1px solid Black '})})

Lose focus
CSS styles can be added by addclass ()
$ ("input[@type =text],textarea,input[@type =password]")
. blur (Function () {$ (this). CSS ({background: ' White ', border: "1px solid Black"});
}

});



HTML code

<div id= "Display" >demo</div>
<div id= "Test" >
<p>adfa<a>dfasfa</a>sdfasdf</p>
<p>adfadfasfasdfasdf</p>
<p>adfadfasfasdfasdf</p>
<p>adfadfasfasdfasdf</p>
</div>
<form id= "Theform" >
Isstring<div><input type= "text" id= "username" onblur= "isstring (this.value)"/></div>
Isinteger<div><input type= "text" id= "password" onblur= "Isinteger (this.value)"/></div>
Istelephone<div><input type= "text" id= "Telephone" onblur= "Istelephone (this.value)"/></div>
Ismobile<div><input type= "text" id= "mobile" onblur= "IsMobile (this.value)"/></div>
Isemail<div><input type= "text" id= "email" onblur= "Isemail (this.value)"/></div>
Isuri<div><input type= "text" id= "uri" onblur= "Isuri (this.value)"/></div>
<div><input type= "button" value= "Validata" onclick= "return Validata ();" /></div>
</form>

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.