JavaScript Regular Expressions (Chinese, numerals, letters)

Source: Internet
Author: User


New

var re = new RegExp (' Test ', ' GI ');
var re =/test/gi;

function
Test ();//bool
exec ();//string match once and return
Match ();//array multiple matches and returns
Search ();//similar to indexof ()
replace ();//replacement
Split ();//Split

Useful predefined classes
. [^nr]//does not contain any characters for line wrapping
d [0-9]
D [^0-9]
s [tnx0bfr]//null character, line feed
S [^tnx0bfr]//not empty, not wrapped
w [a-za-z0-9]
W [^a-za-z0-9]

Quantifiers

? {0,1}
* {0,}
+ {1,}
{n} must appear n times
{n,m} appears at least n times, but not more than m times
{N,} appears at least n times

Greed: "{m,n}", "{m,}", "?", "*", "+"
(d) (w+) "w+" will match all characters "Xxxdxxxd" after the first "D"
(d) (w+) (d) "w+" will match all characters "xxxdxxx" between the first "D" and the Last "D". Although "w+" can also match the last "D", in order for the entire expression to match successfully, "w+" can "yield" the last "D" that it could have matched.

Lazy: "{m,n}?", "{m,}?", "??", "*?", "+?"
(d) (w+?) "w+?" will match as few characters as possible after the first "D", and the result is: "w+?" matches only one "X".
(d) (w+?) (d) In order for the entire expression to be successful, "w+" has to match "xxx" to match the "D" behind, so that the entire expression matches successfully. Therefore, the result is: "w+?" matches "xxx"

Group
var test = ' ([1-2]) ' ([A-z]) ';

Test.replace (Reg,function (R0,R1) {
Console.log (R1)//available R0,R1 for any action
Console.log (regexp.$1);//equivalence R1
});

Matching regular expressions for Chinese characters: [U4E00-U9FA5]
Match Double-byte characters (including Chinese characters): [^x00-xff]

<script>
function Ischinese (temp)
{
var re =/[^u4e00-u9fa5]/;
if (re.test (temp)) return false;
return true;
}
Alert (Ischinese ("Chinese"));
</script>

Example

<script>
<!--
function Checkstr (str) {
[u4e00-ufa29]| [UE7C7-UE7F3] Encoding range
var Re1 = new RegExp ("^" ([u4e00-ufa29]|[ ue7c7-ue7f3]| [A-za-z0-9]) *$");
if (!re1.test (str)) {
Alert ("No");
return false;
}
Alert ("Yes");
return true;
}
-->
</script>

Number is regular

Cases:
/** positive integer Matching expression * *
var pattern=/^[0-9]*[1-9][0-9]*$/;
var flag = Pattern.test (a);
If a is a positive integer, the Boolean value of flag is true, and if a is not a positive integer, the Boolean value of flag is false.

/** floating-point matching expression * *
var pattern =/^ ([-]) {0,1} ([0-9]) {1,} ([.]) {0,1} ([0-9]) {0,}$/;

/** floating-point number matching expression and only two decimal digits are reserved * *
var pattern=/^-?d+[.d]?d{0,2}$/;

/** positive floating-point number matching expression * *
var pattern=/^ ([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 and retains two-bit decimal matching expression * *
var pattern=/^ ([1-9]d* (. d?[ 0-9]?) | (0.[1-9][0-9]) | (0.[0][1-9]) $/;

/** negative floating-point number matching expression * *
var pattern=/^ (-([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 and retains two-bit decimal matching expression * *
var pattern=/^-([1-9]d* (. d?[ 0-9]?) | (0.[1-9][0-9]) | (0.[0][1-9]) $/;

/** negative floating-point number +0 matching expression * *
var pattern=/^ (-d+ (. d+)?) | (0+ (. 0+)?)) $/;

/** negative floating-point number +0 and retains a two-digit decimal matching expression * *
var pattern=/^ ((-d+[.d]?d{0,2}) | ( 0+ (. 0+)?) $/;

Positive floating-point +0 matching expression
var pattern =/^d+ (. d+)? $/;

/** positive floating-point +0 match expression and keep only two decimal digits * *
var pattern=/^d+[.d]?d{0,2}$/;

/** integer Matching expression * *
var pattern=/^-?d+$/;

/** positive integer Matching expression * *
var pattern=/^[0-9]*[1-9][0-9]*$/;

/** negative integer matching expression * *
var pattern=/^-[0-9]*[1-9][0-9]*$/;

/** positive integer +0 matching expression * *
var pattern=/^d+$/;

/** negative integer +0-matched expression * *
var pattern=/^ (-d+) | ( 0+)) $/;

/** is full of digital matching expression * *
var pattern=/^[0-9]{1,20}$/

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.