Regular/\d/g

Source: Internet
Author: User

Onkeyup= "This.value=this.value.replace (/\d/g, ');"
What's the red consciousness?

What do you mean,/g?

-----------------------------------

/g is a global match

----------------------------------

You can use Perl-style expressions in JS
/expression Content/Parameters
such as/\d/g

G: Global Match

----------------------------------


\d: Similar to [^0-9] non-digital
/g: Global

Replace all non-digits with empty

----------------------------------
/Must be added?

var reg =/expression/img;

Before and after the two "/" is required, just like the syntax rules, as the declaration string
String test = "only a test";
Before and after the "" "must have the same, grammatical rules, no reason to speak

IMG is a mode, optional parameter
I ignore case
M Multi-line mode
G Global Mode----------------------------------
http://bbs.csdn.net/topics/340198344
http://msdn.microsoft.com/zh-cn/library/ae5bf541 (v=vs.90). aspx----------------------------------

Integer or decimal: ^[0-9]+\. {0,1} [0-9] {0,2}$
Only numbers can be entered: "^[0-9]*$".
Only n digits can be entered: "^\d{n}$".
Only numbers with at least n digits can be entered: "^\d{n,}$".
You can enter only the digits of the m~n bit:. "^\d{m,n}$"
Only numbers starting with 0 and non-0 can be entered: "^ (0|[ 1-9][0-9]*) $ ".
You can only enter a positive real number with two decimal places: "^[0-9]+ (. [ 0-9]{2})? $ ".
You can only enter a positive real number with a decimal position: "^[0-9]+ (. [ 0-9]{1,3})? $ ".
You can only enter a non-zero positive integer: "^\+?" [1-9] [0-9]*$].
You can only enter a non-zero negative integer: "^\-[1-9][]0-9" *$.
Only characters with a length of 3 can be entered: "^. {3}$ ".
You can only enter a string consisting of 26 English letters: "^[a-za-z]+$".
You can only enter a string consisting of 26 uppercase English letters: "^[a-z]+$".
You can only enter a string consisting of 26 lowercase English letters: "^[a-z]+$".
You can only enter a string consisting of a number and 26 English letters: "^[a-za-z0-9]+$".
You can only enter a string consisting of a number, 26 letters, or underscores: "^\w+$".
Verify user password: "^[a-za-z]\w{5,17}$" is in the correct format: start with a letter, length between 6~18, can contain only characters, numbers, and underscores.
Verify that it contains ^%& ',; =?$\ ' characters: "[^%& ',; =?$\x22]+".
Only Chinese characters can be entered: "^[\u4e00-\u9fa5]{0,}$"
Verify email Address: "^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$ ".
Verify InternetURL: "^http://([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*)? $ ".
Verify the phone number: "^ (\ (\d{3,4}-) |\d{3.4}-)? \d{7,8}$" The correct format is: "Xxx-xxxxxxx", "xxxx-xxxxxxxx", "xxx-xxxxxxx", "xxx-xxxxxxxx", " XXXXXXX "and" XXXXXXXX ".
Verify the Social Security number (15-bit or 18-digit number): "^\d{15}|\d{18}$".
Validation 12 months of the year: "^ (0?[ 1-9]|1[0-2]) $ "The correct format is:" 01 "~" 09 "and" 1 "~" 12 ".
Verify one months of 31 days: "^ ((0?[ 1-9]) | ((1|2) [0-9]) |30|31) $ "correct format for;" 01 "~" 09 "and" 1 "~" 31 ".
Regular expressions that match Chinese characters: [\U4E00-\U9FA5]

Match double-byte characters (including kanji): [^\x00-\xff]

Application: Calculates the length of a string (a double-byte character length meter 2,ascii character 1)
String.prototype.len=function () {return this.replace (/[^\x00-\xff]/g, "AA"). Length;

Regular expression that matches a blank line: \n[\s|] *\r

Regular expressions Matching HTML tags:< (. *) > (. *) <\/(. *) >|< (. *) \/>

Regular expression matching the leading and trailing spaces: (^\s*) | (\s*$)

Application: There is no trim function like VBScript in JavaScript, and we can use this expression to do the following:

String.prototype.trim = function ()
{
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}

Use regular expressions to decompose and convert IP addresses:

The following is a JavaScript program that matches an IP address with a regular expression and translates an IP address into a corresponding value:

function IP2V (IP)
{
re=/(\d+) \. (\d+) \. (\d+) \. (\d+)/g//matching regular expressions for IP addresses
if (Re.test (IP))
{
Return Regexp.$1*math.pow (255,3)) +regexp.$2*math.pow (255,2)) +regexp.$3*255+regexp.$4*1
}
Else
{
throw new Error ("not a valid IP address!")
}
}

However, the above program without regular expression, and directly with the split function decomposition may be more simple, the program is as follows:

var ip= "10.100.20.168"
Ip=ip.split (".")
Alert ("IP value is:" + (IP[0]*255*255*255+IP[1]*255*255+IP[2]*255+IP[3]*1))

Regular expression matching email address: \w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *

Regular expression matching URL URL:/http ([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*)?


Use regular expressions to restrict the entry of text boxes in Web Forms:

Use regular expression restrictions to enter only Chinese: onkeyup= "value=value.replace (/[^\u4e00-\u9fa5]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5]/g, ') "

Restrict only full-width characters with regular expressions: onkeyup= "Value=value.replace (/[^\uff00-\uffff]/g,") "Onbeforepaste=" Clipboarddata.setdata (' Text ', Clipboarddata.getdata (' text '). Replace (/[^\uff00-\uffff]/g, ')) "

Use regular expression restrictions to enter only numbers: onkeyup= "Value=value.replace (/[^\d]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ')) "

Use regular expression restrictions to enter only numbers and English: onkeyup= "Value=value.replace (/[\w]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ')) "

<input onkeyup= "Value=value.replace (/[^\u4e00-\u9fa5\w]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5\w]/g, ') "value=" allow underscore, alphanumeric and kanji ">

<script language= "JavaScript" >
if (document.layers)//Trigger keyboard event
Document.captureevents (event.keypress)

function XZ (thsv,nob) {
if (nob== "2") {
Window.clipboardData.setData ("Text", "" ")
Alert ("Avoid illegal character input, do not copy characters");
return false;
}
if (event.keycode!=8 && event.keycode!=16 && event.keycode!=37 && event.keycode!=38 && event.keycode!=39 && event.keycode!=40) {
thsvv=thsv.value;//the value entered
Thsvs=thsvv.substring (thsvv.length-1);//The last character entered
Thsvss=thsvv.substring (0,thsvv.length-1);//Remove the last error character
if (!thsvs.replace (/[^\u4e00-\u9fa5\w]/g, ') | | event.keycode==189) {//Regular drop symbol and underscore key
Thsv.value= ' do not enter illegal symbols [' +thsvs+ '];
Alert (' Do not enter illegal symbols [' +thsvs+ '] ');
Thsv.value= "";
return false;
}
}
}

</script>

<input onkeyup= "XZ (this,1)" onpaste= "XZ (this,2)" value= "" > Allow alphanumeric letters and kanji

<script language= "JavaScript" >&NBSP;
<!-- 
function MaxLength (field,maxlimit) { 
var j = field.value.replace (/[^\x00-\xff]/g, "* *"). length; 
//alert (j);  
var tempstring=field.value ;  
var tt= "";  
if (J > Maxlimit) { 
for (Var i=0;i<maxlimit;i++) { 
if ( Tt.replace (/[^\x00-\xff]/g, "* *"). Length < Maxlimit)  
TT = TEMPSTRING.SUBSTR (0,i+1);  
else 
break; 

if (Tt.replace (/[^\x00-\xff]/g, "* *"). Length > Maxlimit)  
Tt=tt.substr (0 , tt.length-1);  
Field.value = tt; 
}else{ 
;  


</script

Single-line text box control <br/>
<input type= "text" id= "Text1" name= "Text1" onpropertychange= "MaxLength (this, 5)" ><br/>
Multiline text box control: <br/>
<textarea rows= "14"
cols= "id=" Textarea1 "name=" Textarea1 "onpropertychange=" MaxLength (This,) "></textarea><br/>

Control the contents of the form can only enter numbers, Chinese ....
<script>
function test ()
{
if (DOCUMENT.A.B.VALUE.LENGTH>50)
{
Alert ("Cannot exceed 50 characters!") ");
Document.a.b.focus ();
return false;
}
}
</script>
<form name=a onsubmit= "return Test ()" >
<textarea name= "B" cols= "+" wrap= "VIRTUAL" rows= "6" ></textarea>
<input type= "Submit" name= "Submit" value= "Check" >
</form>

Can only be Chinese characters
<input onkeyup= "Value=value.replace (/[^\u4e00-\u9fa5]/g, ')" >

can only be English characters
<script language=javascript>
function Onlyeng ()
{
if (! ( EVENT.KEYCODE>=65&&EVENT.KEYCODE<=90))
Event.returnvalue=false;
}
</script>

<input onkeydown= "Onlyeng (); >
<input name= "Coname" type= "text" size= "" maxlength= "+" class= "Input2" onkeyup= "Value=value.replace (/[\w]/g, ') "Onbeforepaste=" clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ')) ">
Can only be a number
<script language=javascript>
function Onlynum ()
{
if (! ( (event.keycode>=48&&event.keycode<=57) | | (event.keycode>=96&&event.keycode<=105)))
Consider the numeric keys on the keypad
Event.returnvalue=false;
}
</script>

<input onkeydown= "Onlynum (); >

can only be English characters and numbers
<input onkeyup= "Value=value.replace (/[\w]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ')) ">

Verify to email format
<script Language=javascript runat=server>
function Isemail (stremail) {
if (Stremail.search (/^\w+ (-\w+) | ( \.\w+)) *\@[a-za-z0-9]+ ((\.| -) [a-za-z0-9]+] *\. [a-za-z0-9]+$/)! =-1)
return true;
Else
Alert ("Oh");
}
</SCRIPT>
<input type=text onblur=isemail (this.value) >

Mask keyword (sex, fuck)-Modified
<script language= "JavaScript1.2" >
function Test () {
if ((A.b.value.indexof ("sex") = = 0) | | (A.b.value.indexof ("fuck") = = 0)) {
Alert ("Four Graces and three Passions");
A.b.focus ();
return false;}
}
</script>
<form name=a onsubmit= "return Test ()" >
<input Type=text name=b>
<input type= "Submit" name= "Submit" value= "Check" >
</form>


You can only enter numbers in the Limit text box
<input onkeyup= "if (event.keycode!=37 && event.keycode! =) Value=value.replace (/\d/g, ');" Onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/\d/g, ')" >

<pixtel_mmi_ebook_2005>2 </PIXTEL_MMI_EBOOK_2005>



Regular/\d/g

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.