Regular Expression replacement: the input box can only contain numbers, Chinese and English commas, and the input box can be in Chinese and English.

Source: Internet
Author: User

Regular Expression replacement: the input box can only contain numbers, Chinese and English commas, and the input box can be in Chinese and English.

This is a very simple small feature, because the details have been tossing for a while, so you must be careful.

The implementation code is also relatively simple. For details, refer

Core code:

function renumdou(str){var regexp = /[^\d,,]]*/g;newstr=str.replace(regexp,"");return newstr}

The Helper house editor shares with you another good code:

Automatic detection of digital replacement Regular Expression

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <input onkeyup='check(this)'/> </BODY> <script>function check(obj){var sreg = /^-+.*/g;var zero = /^0[1-9]+\.*\d*/g;var val = obj.value;var plus = '';if(sreg.test(val)){val = val.replace(/-+/g,'');plus = '-';}val = val.replace(/\s+/g,'');if(/^\.+.*$/.test(val)){val = '';}val = val.replace(/[^\d\.]/,'');val = val.replace(/(^\d+\.{1})(\d*).*/g,'$1$2');val = val.replace(/(^\d+\.\d{3})\d*/g,'$1');val = val.replace(/^[0]*(0{1})([1-9]*)(.*)/g,'$1$2$3');if(zero.test(val)){val = val.replace(/0([1-9]+)(.*)$/,'$1$2');}obj.value = plus+val;} </script></HTML>

RegExp instructions

I. How to Create a regular expression
1. Text format:
/Pattern/flags (I .e.: // mode/flag)
2. Use RegExp constructor as follows:
New RegExp ("pattern" [, "flags"]) (I .e., new RegExp ("Mode" [, "tag"])

Parameters:
Pattern: indicates the text of a regular expression.
Flags: If this item is specified, flags can be one of the following:

G: global match (exact match)
I: ignore case (case insensitive)
Gi: both global match and ignore case (matching all possible values, case Insensitive)
Note: Do not use quotation marks for parameters in the text format. Use quotation marks for parameters that are sufficient for generator functions. So the following expression
Is equivalent:
/AB + c/I ============================== new RegExp ("AB + c", "I ")

Description:
When using the constructor function to create a regular expression, you must use a normal string to avoid the rule (adding the leading character \ To the string.
For example, the following two statements are equivalent:
Re = new RegExp ("\ w + ");
Re =/\ w +/

Note: RegExp has a $ attribute preset.
$1,..., $9 attributes
Match a substring enclosed in parentheses, if any.
Is the RegExp attribute
Static, read-only
Available in JavaScript 1.2, NES 3.0 and later versions
Description: Because input is a static attribute, it is not an attribute of an individual regular expression object. You can use RegExp. input to access this
Attribute.
The number of substrings that can be added with parentheses is unrestricted, but the regular expression object can only keep the last nine strings. If you want to access all
The matching string in parentheses. You can use the returned array.

<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <script language = "JavaScript1.2 "> var regexp = new RegExp (" (\ w +) \ s (\ w +) "); str =" John Smith "; newstr = str. replace (regexp, "$2"); newstr2 = str. replace (regexp, "$1"); document. write ("original string:" + str + "<br/>"); document. write (newstr + "<br/>"); document. write (newstr2 + "<br/>"); document. write ('$1 =' + RegExp. $1 + "$2 =" + RegExp. $2); </SCRIPT> </HEAD> <BODY> </HTML>

2. The match () method can be used to search for a specified value in a string or to find a match between one or more regular expressions. It returns the specified value, not the position of the string.

Syntax
StringObject. match (searchvalue)
StringObject. match (regexp) parameter description
Searchvalue is required. Specifies the string value to be retrieved.
Regexp is required. Specifies the RegExp object of the pattern to be matched. If this parameter is not a RegExp object, you must first pass it to the RegExp constructor and convert it to a RegExp object.
Return Value
An array that stores matching results. The content of this array depends on whether regexp has a global flag.
Description
The match () method retrieves the stringObject string to find one or more texts that match regexp. The behavior of this method depends largely on whether regexp has a flag.

If regexp does not mark g, the match () method can only perform one match in stringObject. If no matching text is found, match () returns null. Otherwise, it returns an array containing information related to the matched text it finds.

Match instance:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  <HTML>  <HEAD>   <TITLE> New Document </TITLE>   <SCRIPT LANGUAGE="JavaScript1.2">  var str="1 plus 2 equal 3";  var str2="11/23/55";  var results=str.match(new RegExp("\\d+","gi"));  for(var i=0;i<results.length;i++){    document.write(results[i]+"<br/>");  }  var res=str2.match(new RegExp("(\\d\\d?)/(\\d\\d?)/(\\d\\d)"));  if(str2.length == res[0].length){    document.write(res[1]+"<br/>");    document.write(res[2]+"<br/>");    document.write(res[3]+"<br/>");  }  </SCRIPT>  </HEAD>  <BODY>  </BODY>  </HTML>    function dateCheck(value) {    re = new RegExp("(\\d\\d?)/(\\d\\d?)/(\\d\\d)");    var result = value.match (re);    if (result){      if (result[0].length != value.length){        alert ("Wrong date format. The correct format should be MM/dd/yy.")        return false;      }else{        var t = result[3];        var y = parseInt("20" + t);        var m = parseInt(result[1], 10) - 1;        var day = parseInt(result[2], 10);          var d = new Date(y, m, day);        if (d.getFullYear() != y || d.getMonth() != m || d.getDate() != day){          alert ("error date!")          return false;        }else{          var sm = result[1].length == 1?'0' + result[1]:result[1];          var sday = result[2].length == 1?'0' + result[2]: result[2];          var sy = result[3];          else             return sm + '/' + sday + '/' + sy;        }      }    }else{      alert ("Wrong date format. The correct format should be MM/dd/yy.");      return false;    }  } 

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.