JS Regular Expression

Source: Internet
Author: User

JS Regular Expression

Time: 2016-04-20-10:44:47

Author: zhongxia

Regular expressions are a very common knowledge point in JS verification. Validation of various forms, string validation, and so on can be quickly implemented using regular expressions

The usual way

Example: Verifying the ZIP code

function Ispostcode (str) {
var re =/^\d{6}$/;//different validation, using different regular expressions, as for cue information, you can customize
return re.test (str);

ispostcode (' 1231231 ') false
Ispostcode (' 123412) TrueSyntax for regular expressions
  1. var reg = new RegExp(‘zhongxia‘,‘i‘)Whether it contains zhongxia, case insensitive

    Parameter 1: matching string parameter 2: Matching option flag: I case-insensitive G global search m multi-line lookup

  2. Of course, there's another way to define regular expressions.var reg = /a/i
  3. A few common methods:
    1. Test returns TRUE,FALSE, checksum, most commonly used
    2. EXEC mismatch returns null
    3. Match
    4. Replace
    5. Search does not match return-1
    6. Split
  4. Metacharacters must be transferred with meta characters: ( [ { \ ^ $ | ) ? * + . var re =/?/(Error)
    var re =/\?/(Yes)
  5. Use the RegExp constructor with the regular expression literal to create a regular expression note the point var str = "\?";
    alert (str);//Output only?
    var re =/\?/;//will match?
    Alert (Re.test (str));//true
    Re = new RegExp ("\?"); /error because this is equivalent to re =/\?/
    Re = new RegExp ("\ \"); /correct, will match?
    Alert (Re.test (str));//true
  6. Commonly used/^start///caret (^) to represent the starting position of a character
    /start$///$ indicates where the character ends

    Determine if the character entered is an English lettervar reg=/^[a-zA-Z]+$/;

    Determines whether the input character is an integervar reg=/^[-+]?\d*$/;

    Determine if the character entered is: a-z,a-z,0-9var reg=/^[a-zA-Z0-9_]+$/;

    Determines whether the character entered is in Chinesevar reg=/^[\u0391-\uFFE5]+$/;

    Determine if the input email format is correctvar reg=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

    Determine if the input ZIP code (six-bit only) is correctreg=/^\d{6}$/;

    Phone:/^ ((\d{2,3}) | ( \d{3}-))? ((0\d{2,3}) |0\d{2,3}-)? [1-9]\d{6,7} (-\d{1,4})? $/

    Mobile:/^ (((\d{2,3)) | ( \d{3}-))? 13\d{9}$/

    URL:/^http:\/\/[a-za-z0-9]+. [a-za-z0-9]+[\/=\?%-&_~ ' @[]\ ': +!] ([^<>\ "\"])$/

    Idcard:/^\d{15} (\d{2}[a-za-z0-9])? $/


    Some special amount:/^ ((\d{1,3} (, \d{3}) | ( \d+)) (. \d{2})? $///Description: Except for "XXX xx,xxx xx,xxx.00" format

JS Regular Expression

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.