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
var reg = new RegExp(‘zhongxia‘,‘i‘)
Whether it contains zhongxia, case insensitiveParameter 1: matching string parameter 2: Matching option flag: I case-insensitive G global search m multi-line lookup
- Of course, there's another way to define regular expressions.
var reg = /a/i
- A few common methods:
- Test returns TRUE,FALSE, checksum, most commonly used
- EXEC mismatch returns null
- Match
- Replace
- Search does not match return-1
- Split
- Metacharacters must be transferred with meta characters:
( [ { \ ^ $ | ) ? * + .
var re =/?/(Error)
var re =/\?/(Yes)
- 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
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])? $/
QQ:/^[1-9]\d{4,8}$/
Some special amount:/^ ((\d{1,3} (, \d{3}) | ( \d+)) (. \d{2})? $///Description: Except for "XXX xx,xxx xx,xxx.00" format
JS Regular Expression