Home >Web Front-end >JS Tutorial >Special characters in regular expressions

Special characters in regular expressions

巴扎黑
巴扎黑Original
2017-03-18 10:31:581708browse

1.JS regular expression collection (upon)

Special characters in regular expressions
Character meaning
is used as a transposition, that is, the characters usually after "" are not interpreted according to their original meaning, such as /b/ matches the character "b", when a backslash is added in front of b After the stem /b/, the meaning is changed to match the boundary of a word.
-or-
Reduction of regular expression functional characters, such as "*" matching the metacharacter before it 0 or more times, /a*/ will match a, aa, aaa, after adding "", /a* / will only match "a*".
^ matches an input or the beginning of a line, /^a/ matches "an A", but does not match "An a"
$ matches an input or the end of a line, /a$/ matches "An a", but does not match "an A"
* matches the preceding metacharacter 0 or more times, /ba*/ will match b,ba,baa,baaa
+ matches the preceding metacharacter 1 or more times, /ba*/ will match ba,baa ,baaa
? Matches the preceding metacharacter 0 or 1 times, /ba*/ will match b,ba
(x) matches x and saves x in a variable named $1...$9
x|y matches x or y
{n} matches exactly n times
{n,} matches more than n times
{n,m} matches n-m times
[xyz] character set (character set), matches any character (or element) in this set characters)
[^xyz] does not match any character in this set
[b] matches a backspace character
b matches a word boundary
B matches a non-word boundary
cX Here, X is a control character, /cM/ matches Ctrl-M
d matches an alphanumeric character, /d/ = /[0-9]/
D matches a non-alphanumeric character, /D/ = /[^0-9]/
n matches a newline The symbol
r matches a carriage return character
s matches a blank character, including n, r, f, t, v, etc.
S matches a non-blank character, equal to /[^nfrtv]/
t matches a tab character
v Matches a double tab character
w matches a character that can form a word (alphanumeric, this is my free translation, including numbers), including underscores, such as [w] matching the 5 in "$5.98", which is equal to [a-zA- Z0-9]
W matches a character that cannot form a word. For example, [W] matches the $ in "$5.98", which is equal to [^a-zA-Z0-9].

2.JS Regular Expression Collection (Part 2)
A regular expression is an object that describes a character pattern.
JavaScript’s RegExp object and String object define ways to use regular expressions to perform powerful pattern matching and text retrieval and replacement functions.
In JavaScript, regular expressions are represented by a RegExp object. Of course, you can use A RegExp() constructor to create a RegExp object, or a special syntax added in JavaScript 1.2 to create a RegExp object. Just like a string literal is defined as a character enclosed in quotes, a regular expression literal Quantities are also defined as characters contained between a pair of slashes (/). So, JavaScript might contain the following code:
var pattern = /s$/;
This line of code creates a new RegExp object and Assign it to the variable pattern. This special RegExp object matches all strings ending with the letter "s". You can also define an equivalent regular expression using RegExp(), the code is as follows:
var pattern = new RegExp ("s$");
Whether you use a regular expression literal or the constructor RegExp(), it is relatively easy to create a RegExp object. The more difficult task is to use regular expression syntax to describe the pattern of characters.
JavaScript uses a fairly complete subset of the Perl language regular expression syntax.
The pattern specification of a regular expression is composed of a series of characters. Most characters (including all alphanumeric characters) are described literally Characters that mean to match. In this way, the regular expression /java/ matches all strings containing the substring "java". Although other characters in the regular expression are not matched literally, they are Has a special meaning. The regular expression /s$/ contains two characters.
The first special character "s" matches itself literally. The second character "$" is a special character that matches is the end of the string. So the regular expression /s$/ matches the string ending with the letter "s".
1. Direct character
We have discovered that all letters in the regular expression Characters and numbers match themselves literally. JavaScript's regular expressions also support certain non-alphabetic characters through escape sequences starting with a backslash (). For example, the sequence "n" matches in a string is a literal newline character. In regular expressions, many punctuation characters have special meanings. Here are these characters and their meanings:
Literal characters for regular expressions
Character matching
____________________________
Alphanumeric characters themselves
f form feed character
n line feed character
r carriage return
t tab character
v vertical tab character
/ a / direct quantity
a direct quantity
. a. direct quantity
* a * direct quantity
+ a + direct quantity
? a ? direct quantity
| a | direct quantity
( a ( direct quantity
) a ) direct quantity
[ a [ direct quantity
] a ] literal
{ a { literal
} a } literal
XXX ASCII code character specified by the decimal number XXX
Equivalent to t, cJ is equivalent to n
____________________________________________
If you want to use special punctuation marks in regular expressions, you must add a "" before them.

2. Character class Put separate direct characters Character classes can be combined into square brackets. A character class matches any character it contains, so the regular expression / [abc] / matches any of the letters "a", "b", "c" Matches all. In addition, you can also define negative character classes. These classes match all characters except those characters contained within square brackets. When defining the negative character tip, a ^ symbol should be used as the starting point from the left square bracket. The first character of . The set of regular expressions is /[a-zA-z0-9]/.
Because certain character classes are very commonly used, JavaScript’s regular expression syntax contains some special characters and escape sequences to represent These commonly used classes. For example, s matches spaces, tabs and other whitespace characters, s matches any character except whitespace characters.
Regular gray-type character class
Character matching
____________________________________________________
[ ...] Any character within brackets
[^...] Any character not within brackets
. Any character except line breaks, equivalent to [^n]
w Any single-word character, etc. Equivalent to [a-zA-Z0-9]
W Any non-single character, equivalent to [^a-zA-Z0-9]
s Any whitespace character, equivalent to [t n r f v]
S Any non-whitespace character, Equivalent to [^ t n r f v]
d Any number, equivalent to [0-9]
D Any character other than numbers, equivalent to [^0-9]
[b] A backspace literal (special case )
______________________________________________________________

3. Copy Using the above regular expression syntax, two-digit numbers can be described as / d d /, and four-digit numbers can be described as / d d d d /. But we don’t have a method to use it yet Describes a number or a string with any number of digits. The string consists of three characters followed by a digit. These complex patterns use regular expression syntax that specifies what each element of the expression should be. The number of repetitions.
The characters specified to be copied always appear after the pattern they are applied to. Since certain copy types are quite common, there are some special characters specially used to represent them. For example: + matches the pattern before copying. A pattern one or more times. The following table lists the copy syntax. Let’s look at an example first:
/d{2, 4}/ //Match numbers between 2 and 4.
/w{3} d? / //Match three single characters and an arbitrary number.
/s+javas+/ //Match the string "java", and there can be one or more spaces before and after the string.
/[^"] * / / / Matches zero or more non-quote characters.

Copy characters of regular expressions Character meaning
_______________________________________________________________
{n, m} matches the previous item at least n times, but not more than m times
{n, } matches The previous item n times, or multiple times
{n} matches the previous item exactly n times
? Matches the previous item 0 or 1 times, which means the previous item is optional. Equivalent to {0, 1 }
* matches the previous item 0 or more times. Equivalent to {0,}
____________________________________________________________

4. Selection, grouping and reference The syntax of regular expressions also includes specifying selection items, grouping subexpressions and Special characters referencing the previous subexpression. The character | is used to separate characters for selection. For example: /ab|cd|ef/ matches the string "ab", or the string "cd", or "ef ". /d{3}|[a-z]{4}/ matches either a three-digit number or four lowercase letters. Parentheses have several functions in regular expressions. Its main function is to separate separate The items are grouped into subexpressions so that those items can be processed as an independent unit using *, + or ?. For example: /java(script) ?/ matches the string "java", followed by both There may or may not be "script". / (ab|cd) + |ef) / Matches either the string "ef" or one or more repetitions of the string "ab" or "cd" .
The second use of parentheses in regular expressions is to define subpatterns within a complete pattern. When a regular expression successfully matches a target string, the portion of the target string that matches the subpattern in parentheses can be extracted. For example, assume that the pattern we are retrieving is one or more letters followed by a single digit. or multiple digits, then we can use the pattern /[a-z]+d+/. But since it is assumed that we really care about the digits at the end of each match, then if we put the numeric part of the pattern in parentheses (/[a-z] + (d+)/) , we can extract a number from any match retrieved, which we will parse later.
Another use of parenthesized subexpressions is to allow us to use them in the same regular expression The back of the expression refers to the previous subexpression. This is achieved by adding one or more digits after the string. The number refers to the position of the subexpression in the regular expression. For example: 1 reference is the subexpression of the first bracket. 3 refers to the subexpression of the third bracket. Note that since subexpressions can be nested in other subexpressions, their positions are counted. The position of the left bracket.
For example: In the following regular expression is specified as 2:
/([Jj]ava([Ss]cript)) sis s (funw*) /
for the previous subexpression in the regular expression The reference of a regular expression does not specify the pattern of the subexpression, but the text that matches that pattern. In this way, the reference is not only a shortcut to help you enter the repeated part of the regular expression, it also implements a Convention, that is, each separate part of a string contains exactly the same characters. For example: the following regular expression matches all characters located within single or double quotes. However, it requires opening and closing quotes Match (for example, both are double quotes or both are single quotes):
/[' "] [^ ' "]*[' "]/
If the opening and closing quotes are required to match, we can use the following quotes:
/( [' "] ) [^ ' "] * 1/
1 matches the pattern matched by the first parenthesized subexpression. In this example, it implements a convention, which is start The quote must match the closing quote. Note that if the backslash is followed by more digits than the number of subexpressions in the bracket, it will be parsed as a decimal escape sequence rather than a quote. You You can stick to using the full three characters to represent escape sequences, which will avoid confusion. For example, useb matches the boundary of a word. In short, it is the position between the characters w and w (note: [b] matches the backspace character)
B matches the characters that are not the boundary of the word
______________________________________________________________
6. Attributes
There is one last element about the regular expression syntax, which is the attributes of the regular expression, which describes the rules of advanced pattern matching. Unlike other regular expression syntax, the attributes are outside the / symbol. Illustrated. That is, they do not appear between two slashes, but after the second slash. JavaScript 1.2 supports two attributes. Attribute i indicates that pattern matching should be case-insensitive. Attribute g indicates that pattern matching Should be global. In other words, all matches in the retrieved string should be found. These two properties can be combined to perform a global, case-insensitive match.
For example: To perform a match of different sizes For a sensitive search to find the first specific value of the word "java" (or "java", "JAVA", etc.), we can use the size-insensitive regular expression /b javab/i. If we want to find the first specific value in a string To find all the specific values ​​of "java", we can also add the attribute g, that is, /b java b/gi.
JS regular expression collection [3]
The following are the attributes of regular expressions:
Character meaning
__________________________________________
i Perform case-insensitive matching
g Perform a global match, in short, find all matches instead of stopping after finding the first one
_______________________________________________
Except for attributes g and i, regular expressions The formula has no other properties like attributes. If the static attribute multiline of the constructor RegExp is set to true, then the pattern matching will be performed in multiline mode. In this mode, the anchor characters ^ and $ do not match It only searches the beginning and end of the string, but also matches the beginning and end of a line inside the search string. For example: the pattern /Java$/ matches "Java", but does not match "Javanis fun". If we set multiline attribute, then the latter will also be matched:
RegExp.multiline = true;
Determine whether a string is in the format of an email in JAVASCRIPT:

if(formname.email.value!=formname.email.value.match(/^\w +[@]\w +[.][\w.] +$/)) 
{ 
alert("您的电子邮件格式错误!"); 
formname.email.focus(); 
return false; 
} 
[RED]function dateVerify(date){ 
var reg = /^(\d{4})(-)(\d{2})\2(\d{2})$/; 
var r = date.match(reg); 
if(r==null) return false; 
var d= new Date(r[1], r[3]-1,r[4]); 
var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate(); 
date=r[1]+r[2]+((r[3]-1)+1)+r[2]+((r[4]-1)+1); 
return newStr==date; 
}

javascript的17种正则表达式 
"^\\d+$"  //非负整数(正整数 + 0) 
"^[0-9]*[1-9][0-9]*$"  //正整数 
"^((-\\d+)|(0+))$"  //非正整数(负整数 + 0) 
"^-[0-9]*[1-9][0-9]*$"  //负整数 
"^-?\\d+$"    //整数 
"^\\d+(\\.\\d+)?$"  //非负浮点数(正浮点数 + 0) 
"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数 
"^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$"  //非正浮点数(负浮点数 + 0) 
"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数 
"^(-?\\d+)(\\.\\d+)?$"  //浮点数 
"^[A-Za-z]+$"  //由26个英文字母组成的字符串 
"^[A-Z]+$"  //由26个英文字母的大写组成的字符串 
"^[a-z]+$"  //由26个英文字母的小写组成的字符串 
"^[A-Za-z0-9]+$"  //由数字和26个英文字母组成的字符串 
"^\\w+$"  //由数字、26个英文字母或者下划线组成的字符串 
"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"    //email地址 
"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$"  //url 
JavaScript中的正则表达式
正则表达式对象的属性及方法 
预定义的正则表达式拥有有以下静态属性:input, multiline, lastMatch, lastParen, leftContext, rightContext和$1到$9。其中input和multiline可以预设置。其他属性的值在执行过exec或test方法后被根据不同条件赋 以不同的值。许多属性同时拥有长和短(perl风格)的两个名字,并且,这两个名字指向同一个值。(JavaScript模拟perl的正则表达式) 
正则表达式对象的属性 
属性 含义 
$1...$9 如果它(们)存在,是匹配到的子串 
$_ 参见input 
$* 参见multiline 
$& 参见lastMatch 
$+ 参见lastParen 
$` 参见leftContext 
$''          参见rightContext 
constructor    创建一个对象的一个特殊的函数原型 
global       是否在整个串中匹配(bool型) 
ignoreCase     匹配时是否忽略大小写(bool型) 
input        被匹配的串 
lastIndex     最后一次匹配的索引 
lastParen     最后一个括号括起来的子串 
leftContext    最近一次匹配以左的子串 
multiline     是否进行多行匹配(bool型) 
prototype     允许附加属性给对象 
rightContext    最近一次匹配以右的子串 
source       正则表达式模式 
lastIndex     最后一次匹配的索引 
正则表达式对象的方法 
方法 含义 
compile      正则表达式比较 
exec        执行查找 
test        进行匹配 
toSource      返回特定对象的定义(literal representing),其值可用来创建一个新的对象。重载Object.toSource方法得到的。 
toString      返回特定对象的串。重载Object.toString方法得到的。 
valueOf      返回特定对象的原始值。重载Object.valueOf方法得到 
例子 
334f6305b6ca5b80c0fa1a5fd0526a69 
var myReg = /(w+)s(w+)/; 
var str = "John Smith"; 
var newstr = str.replace(myReg, "$2, $1"); 
document.write(newstr); 
2cacc6d41bbb37262a98f745aa00fbf0 
将输出"Smith, John" 
JS正则表达式大全 

/********************************************************************************* 
* EO_JSLib.js 
* javascript正则表达式检验 
**********************************************************************************/ 
//校验是否全由数字组成 
function isDigit(s) 
{ 
var patrn=/^[0-9]{1,20}$/; 
if (!patrn.exec(s)) return false 
return true 
} 
//校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串 
function isRegisterUserName(s) 
{ 
var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/; 
if (!patrn.exec(s)) return false 
return true 
} 
//校验用户姓名:只能输入1-30个以字母开头的字串 
function isTrueName(s) 
{ 
var patrn=/^[a-zA-Z]{1,30}$/; 
if (!patrn.exec(s)) return false 
return true 
} 
//校验密码:只能输入6-20个字母、数字、下划线 
function isPasswd(s) 
{ 
var patrn=/^(\w){6,20}$/; 
if (!patrn.exec(s)) return false 
return true 
} 
//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-” 
function isTel(s) 
{ 
//var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?(\d){1,12})+$/; 
var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/; 
if (!patrn.exec(s)) return false 
return true 
} 
//校验手机号码:必须以数字开头,除数字外,可含有“-” 
function isMobil(s) 
{ 
var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/; 
if (!patrn.exec(s)) return false 
return true 
} 
//校验邮政编码 
function isPostalCode(s) 
{ 
//var patrn=/^[a-zA-Z0-9]{3,12}$/; 
var patrn=/^[a-zA-Z0-9 ]{3,12}$/; 
if (!patrn.exec(s)) return false 
return true 
} 
//校验搜索关键字 
function isSearch(s) 
{ 
var patrn=/^[^`~!@#$%^&*()+=|\\\][\]\{\}:;&#39;\,.<>/?]{1}[^`~!@$%^&()+=|\\\][\]\{\}:;&#39;\,.<>?]{0,19}$/; 
if (!patrn.exec(s)) return false 
return true 
} 
function isIP(s) //by zergling 
{ 
var patrn=/^[0-9.]{1,20}$/; 
if (!patrn.exec(s)) return false 
return true 
}

正则表达式regular expression详述
正则表达式是regular expression,看来英文比中文要好理解多了,就是检查表达式符 
不符合规定!!正则表达式有一个功能十分强大而又十分复杂的对象RegExp,在JavaScript1.2 版本以上提供。 
下面我们看看有关正则表达式的介绍: 
正则表达式对象用来规范一个规范的表达式(也就是表达式符不符合特定的要求,比如是不是Email地址格式等),它具有用来检查给出的字符串是否符合规则的属性和方法。 
除此之外,你用RegExp构造器建立的个别正则表达式对象的属性,就已经预先定义好了正则表达式对象的静态属性,你可以随时使用它们。 
核心对象: 
在JavaScript 1.2, NES 3.0以上版本提供。 
在JavaScript 1.3以后版本增加了toSource方法。 
建立方法: 
文字格式或RegExp构造器函数。 
文字建立格式使用以下格式: 
/pattern/flags即/模式/标记 
构造器函数方法使用方法如下: 
new RegExp("pattern"[, "flags"])即new RegExp("模式"[,"标记"]) 
参数: 
pattern(模式)  ——表示正则表达式的文本 
flags(标记) ——如果指定此项,flags可以是下面值之一: 
g: global match(全定匹配) 
i: ignore case(忽略大小写) 
gi: both global match and ignore case(匹配所有可能的值,也忽略大小写) 
注意:文本格式中的参数不要使用引号标记,而构造器函数的参数则要使用引号标记。所以下面的表达式建立同样的正则表达式: 
/ab+c/i 
new RegExp("ab+c", "i") 
描述:当使用构造函数的时候,必须使用正常的字符串避开规则(在字符串中加入前导字符\ )是必须的。 例如,下面的两条语句是等价的: 
re = new RegExp("\\w+") 
re = /\w+/ 
下面的提供了在正则表达式中能够使用的完整对特殊字符的一个完整的列表和描述。 
表1.3:正则表达式中的特殊字符: 
字符\ ——意义:对于字符,通常表示按字面意义,指出接着的字符为特殊字符,\不作解释。 
例如:/b/匹配字符'b',通过在b 前面加一个反斜杠\,也就是/\b/,则该字符变成特殊字符,表示 
匹配一个单词的分界线。 
或者: ——对于几个字符,通常说明是特殊的,指出紧接着的字符不是特殊的,而应该按字面解释。 
例如:*是一个特殊字符,匹配任意个字符(包括0个字符);例如:/a*/意味匹配0个或多个a。 
为了匹配字面上的*,在a前面加一个反斜杠;例如:/a\*/匹配'a*'。 
字符^ ——意义:表示匹配的字符必须在最前边。 
例如:/^A/不匹配"an A,"中的'A',但匹配"An A."中最前面的'A'。 
字符$ ——意义:与^类似,匹配最末的字符。 
例如:/t$/不匹配"eater"中的't',但匹配"eat"中的't'。 
字符* ——意义:匹配*前面的字符0次或n次。 
例如:/bo*/匹配"A ghost booooed"中的'boooo'或"A bird warbled"中的'b',但不匹配"A goat g 
runted"中的任何字符。 
字符+ ——意义:匹配+号前面的字符1次或n次。等价于{1,}。 
例如:/a+/匹配"candy"中的'a'和"caaaaaaandy."中的所有'a'。 
字符? ——意义:匹配?前面的字符0次或1次。 
例如:/e?le?/匹配"angel"中的'el'和"angle."中的'le'。 
字符. ——意义:(小数点)匹配除换行符外的所有单个的字符。 
例如:/.n/匹配"nay, an apple is on the tree"中的'an'和'on',但不匹配'nay'。 
字符(x) ——意义:匹配'x'并记录匹配的值。 
例如:/(foo)/匹配和记录"foo bar."中的'foo'。匹配子串能被结果数组中的素[1], ..., [n] 返 
回,或被RegExp对象的属性$1, ..., $9返回。 
字符x|y ——意义:匹配'x'或者'y'。 
例如:/green|red/匹配"green apple"中的'green'和"red apple."中的'red'。 
字符{n} ——意义:这里的n是一个正整数。匹配前面的n个字符。 
For example: /a{2}/ does not match the 'a' in "candy," but matches all the 'a's in "caandy," and the first two
'a's in "caaandy.".
Character {n,} - Meaning: n here is a positive integer. Matches at least n previous characters.
For example: /a{2,} does not match the 'a' in "candy", but matches all the 'a's in "caandy" and all the 'a's in "caaaaaaandy."
Characters {n,m} — - Meaning: n and m here are both positive integers. Matches at least n and at most m previous characters.
For example: /a{1,3}/ does not match any character in "cndy", but matches the 'a' in "candy," and the first two
'a' and "caaaaaaandy" in "caandy," The first three 'a's in "caaaaaaandy", note: even if there are many 'a's in "caaaaaaandy", it only matches the first three 'a's, that is, "aaa".
Character [xyz] - Meaning: A list of characters, matching any character in the list. You can specify a range of characters using the hyphen -.
For example: [abcd] is the same as [a-c]. They match the 'b' in "brisket" and the 'c' in "ache".
Character[^xyz] - Meaning: One-character complement, that is, it matches everything except the listed characters. You can use hyphens to indicate a character range.
For example: [^abc] and [^a-c] are equivalent, they first match the 'r' in "brisket" and the 'h' in "chop.".
Character [b] - Meaning: Match a space (not to be confused with b)
Character b - Meaning: Match a dividing line of a word, such as a space (not to be confused with [b])
For example: /bnw/ matches The 'no' in "noonday", /wyb/ matches the 'ly' in "possibly yesterday.".
Character B - Meaning: Match the non-breaking line of a word
For example: /wBn/ matches the 'on' in "noonday", /yBw/ matches the 'ye' in "possibly yesterday.".
Character cX - Meaning: X here is a control character. Matches a string of control characters.
For example: /cM/ matches control-M in a string.
Character d - meaning: matches a number, equivalent to [0-9].
For example: /d/ or /[0-9]/ matches the '2' in "B2 is the suite number.".
Character D - Meaning: Matches any non-number, equivalent to [^0-9].
For example: /D/ or /[^0-9]/ matches the 'B' in "B2 is the suite number.".
Character f ——Meaning: Match a form character
Character n ——Meaning: Match a newline character
Character r ——Meaning: Match a carriage return character
Character s ——Meaning: Match a single white space character, including spaces , tab, form feed, newline character, equivalent to [fnrtv].
For example: /sw*/ matches 'bar' in "foo bar.".
Character S - Meaning: Matches a single character except white space, equivalent to [^ fnrtv].
For example: /S/w* matches 'foo' in "foo bar.".
Character t - Meaning: Match a tab character
Character v - Meaning: Match a leading tab character
Character w - Meaning: Match all numbers, letters and underscores, equivalent to [A-Za-z0 -9_].
For example: /w/ matches the 'a' in "apple,", the '5' in "$5.28," and the '3' in "3D.".
Character W - Meaning: Matches other characters except numbers, letters and underscores, equivalent to [^A-Za-z0-9_].
For example: /W/ or /[^$A-Za-z0-9_]/ matches the '%' in "50%.".
Character n - meaning: n here is a positive integer. The value of n that matches the last substring of a regular expression (counting left parentheses).
For example: /apple(,)sorange1/ matches 'apple, orange' in "apple, orange, cherry, peach.", there is a more complete example below
.
Note: If the number in the left parenthesis is smaller than the number specified by n, n takes the octal escape of the next line as the description.
Characters ooctal and xhex - meaning: ooctal here is an octal escape value, and xhex is a hexadecimal escape value, allowing ASCII codes to be embedded in a regular expression.
Literal notation provides a way to edit regular expressions when the expression is checked. Regular expressions can be kept constant using literal notation. For example, if you use literal notation in a loop to construct a regular expression, the regular expression does not need to be compiled repeatedly.
正则表达式对象构造器,例如,new RegExp("ab+c"),提供正则表达式的运行时编译。当你知道正 则表达式的模式会变化的时候,应该使用构造函数,或者你不知道正则表达式的模式,而它们是从另外 的源获得的时候,比如由用户输入时。一旦你定义好了正则表达式,该正则表达式可在任何地方使用, 并且可以改变,你可以使用编译方法来编译一个新的正则表达式以便重新使用。 
一个分离预先定义的RegExp对象可以在每个窗口中使用;也就是说,每个分离的JavaScript线程运 行以获得自己的RegExp对象。因为每个脚本在一个线程中是不可中断的,这就确保了不同的脚本不会覆 盖RegExp对象的值。 
预定义的RegExp对象包含的静态属性:input, multiline, lastMatch,lastParen, leftContext, rightContext, 以及从$1到$9。input和multiline属性能被预设。其它静态属性的值是在执行个别正则 表达式对象的exec和test方法后,且在执行字符串的match和replace方法后设置的。 
属性 
注意RegExp对象的几个属性既有长名字又有短名字(象Perl)。这些名字都是指向相同的值。Perl是 一种编程语言,而JavaScript模仿了它的正则表达式。 
属性$1, ..., $9 
取得匹配的子串,如果有的话 
属性$_ 
参考input 
属性$* 
参考multiline 
属性$& 
参考lastMatch 
属性$+ 
参考lastParen 
属性$` 
参考leftContext 
属性$' 
参考rightContext 
属性constructor 
指定用来建立对象原型函 
属性global 
决定是否测试正则表达式是否不能匹配所有的字符串,或者只是与最先的冲突。 
属性ignoreCase 
决定试图匹配字符串的时候是否忽略大小写 
属性input 
当正则表达式被匹配的时候,为相反的字符串。 
属性lastIndex 
决定下一次匹配从那里开始 
属性lastMatch 
最后一个匹配的字符 
属性lastParen 
子串匹配的时候,最后一个parenthesized,如果有的话。 
属性leftContext 
最近一次匹配前的子串。 
属性multiline 
是否在串的多行中搜索。 
属性prototype 
允许附加属性到所有的对象 
属性rightContext 
最近一次匹配后的的子串。 
属性source 
模式文本 

方法 
compile方法 
编译一个正则表达式对象 
exec方法 
运行正则表达式匹配 
test方法 
测试正则达式匹配 
toSource方法 
返回一个对象的文字描述指定的对象;你可以使用这个值来建立一个新的对象。不考虑Object.toS 
ource方法。 
toString方法 
返回一个字符串描述指定的对象,不考虑Object.toString对象。 
valueOf方法 
返回指定对角的原始值。不考虑Object.valueOf方法。 
 另外,这个对象继承了对象的watch和unwatch方法 

例子: 
例1、下述示例脚本使用replace方法来转换串中的单词。在替换的文本中,脚本使用全局 RegExp 
对象的$1和$2属性的值。注意,在作为第二个参数传递给replace方法的时候,RegExp对象的$属性的名 
称。 

<SCRIPT LANGUAGE="JavaScript1.2"> 
re = /(\w+)\s(\w+)/; 
str = "John Smith"; 
newstr=str.replace(re,"$2, $1"); 
document.write(newstr) 
</SCRIPT>

显示结果:"Smith, John". 
例2、下述示例脚本中,RegExp.input由Change事件处理句柄设置。在getInfo函数中,exec 方法 
使用RegExp.input的值作为它的参数,注意RegExp预置了$属性。 

<SCRIPT LANGUAGE="JavaScript1.2"> 
function getInfo(abc) 
{ 
re = /(\w+)\s(\d+)/; 
re.exec(abc.value); 
window.alert(RegExp.$1 + ", your age is " + RegExp.$2); 
} 
</SCRIPT> 
  请输入你的姓和年龄,输入完后按回车键。 
<FORM><INPUT TYPE="TEXT" NAME="NameAge" onChange="getInfo(this);"></FORM> 
</HTML>

$1, ..., $9属性 
用圆括号括着的匹配子串,如果有的话。 
是RegExp的属性 
静态,只读 
在JavaScript 1.2, NES 3.0以上版本提供 
描述:因为input是静态属性,不是个别正则表达式对象的属性。你可以使用RegExp.input 访问该 
属性。 
能加上圆括号的子串的数量不受限制,但正则表达式对象只能保留最后9 条。如果你要访问所有的 
圆括号内的匹配字串,你可以使用返回的数组。 
这些属性能用在RegExp.replace方法替换后的字符串(输出结果)。当使用这种方式的时候,不用预 
先考虑RegExp对象。下面给出例子。当正则表达式中没有包含圆括号的时候,该脚本解释成$n的字面意 
义。(这里的n是一个正整数)。 
例如: 
下例脚本使用replace 方法来交换串中单词的位置。在替换后的文本字串中,脚本使用正则表达式 
RegExp对象的$1和$2属性的值。注意:当它们向replace方法传递参数的时候,这里没有考虑 $ 属性的 
RegExp对象的名称。 

<SCRIPT LANGUAGE="JavaScript1.2"> 
re = /(\w+)\s(\w+)/; 
str = "John Smith"; 
newstr=str.replace(re,"$2, $1"); 
document.write(newstr) 
</SCRIPT>

显示的输出结果为:Smith, John。 
JS正则表达式大全 
正则表达式regular expression详述
以下这些不是正则表达式的新增对象请参阅对应的JavaScript对象的属性 $_属性 参考input $*属性 
参考multiline $&属性 参考lastMatch $+属性 参考lastParen $`属性 
参考leftContext $'属性 参考rightContext compile方法 在脚本运行期间编译正则表达式对象 
属于RegExp的方法 在JavaScript 1.2, NES 3.0以上版本提供 语法: 
regexp.compile(pattern[, flags]) 以数: regexp 正则表达式的名称,可以是变量名或文字串。 
pattern 正则表达式的定义文本。 flags 如果指定的话,可以是下面其中的一个: "g": 匹配所有可能的字串 
"i": 忽略大小写 "gi": 匹配所有可能的字串及忽略大小写 描述: 
使用compile方法来编译一个正则表达式 created with the RegExp constructor function。这样 
就强制正则表达式只编译一次,而不是每次遇到正则表达式的时候都编译一次。当你确认正则表达式能 
保持不变的时候可使用compile 方法来编译它(在获得它的匹配模式后),这样就可以在脚本中重复多次使用它。 
你亦可以使用compile 方法来改变在运行期间改变正则表达式。例如,假如正则表达式发生变化, 
你可以使用compile方法来重新编译该对象来提高使用效率。 
使用该方法将改变正则表达式的source, global和ignoreCasesource属性的值。 constructor 
指出建立对象原型的function。注意这个属性的值由函数本身提供,而不是一个字串包含RegExp的name.Property提供。 
在JavaScript 1.1, NES 2.0以上版本提供 ECMA版本ECMA-262 描述:参考Object.constructor. 
exec方法 在指定的字符串运行匹配搜索。返回一个结果数组。 是RegExp的方法 
在JavaScript 1.2, NES 3.0以上版本提供 语法: regexp.exec([str])regexp([str]) 
参数: regexp,正则表达式的名称,可以是一个变量名或文字定义串。 
str,要匹配正则表达式的字符串,如果省略,将使用RegExp.input的值。 
描述:就如在语法描述中的一样,正则表达工的exec方法能够被直接调用(使用regexp.exec(str))或者间接调用(使用regexp(str))。 
假如你只是运行以找出是否匹配,可以使用String搜索方法。 
假如匹配成功,exec方法返回一个数组并且更新正则表达式对象属性的值和预先定义的正则表达式对象、RegExp。如果匹配失败,exec方法返回null。 
请看下例: 8cc9a834f4278df216fc1f34199a18d6 //匹配一个b接着一个或多个d,再接着一个b 
//忽略大小写 myRe=/d(b+)(d)/ig; myArray = myRe.exec("cdbBdbsbz"); 
db271416853c42fd247a57c1a2c29eb6 下面是该脚本的返回值:对象 属性/Index 描述 例子 
myArray 
myArray的内容 ["dbBd", "bB", "d"] 
index 
基于0的匹配index 1 
input 
原始字符串 cdbBdbsbz 
[0] 
最后匹配的字符 dbBd 
[1], ...[n] 
用圆括号括住的匹配字符串,如果有的话。不限制括号的个数。 [1] = bB 
[2] = d 
myRe 
lastIndex 
开始下次匹配操作的index值 5 
ignoreCase 
指出"i"是否使用以忽略大小写 true 
global 
指出是否使用"g"标记来进行匹配所有可能的字串 true 
source 
定义模式的文本字符串 d(b+)(d) 
RegExp 
lastMatch$& 
最后匹配的字符 dbBd 
leftContext$\Q 
最新匹配前面的子串 c 
rightContext$' 
最新匹配后面的子串 bsbz 
$1, ...$9 
圆括号内的匹配子串,如果有的话。圆括号的个数不受限制,但RegExp只能保留最后9个 $1 = bB 
$2 = d 
lastParen $+ 
最后一个加上圆括号的匹配子串,如果有的话 d 
假如你的正则表达式使用了"g"标记,你可以多次使用exec 方法来连续匹配相同的串。当你这样做 
的时候,新的匹配将从由正则表达式的lastIndex 属性值确定的子串中开始。例如,假定你使用下面的脚本: 
8cc9a834f4278df216fc1f34199a18d6 myRe=/ab*/g;str = "abbcdefabh" 
myArray = myRe.exec(str); 
document.writeln("Found "+myArray[0]+". Next match starts at "+myRe.lastIndex) 
mySecondArray = myRe.exec(str);
document.writeln("Found "+mySecondArray[0]+". Next match starts at "+myRe.lastIndex)
db271416853c42fd247a57c1a2c29eb6 This script displays the following results: Found abb. Next match starts at 3
Found ab. Next match starts at 9 Example:
In the following example, the user enters a name and the script performs a matching operation based on the input. Then the array is checked to see if it matches any other user's name.
This script assumes that the registered user's last name has been stored in array A, perhaps obtained from a database. 6a74014ee44f5deb5894267f99b68016
50950bf35e67c33484f0159171ae2eae A = ["zhao","qian","sun","li","liang"]
function lookup() { firstName = /w+ /i(); if (!firstName)
window.alert (RegExp.input + "Illegal input"); else { count=0;
for (i=0;i Enter your last name and press Enter.
632555444b46edf0af44e58213a0ac8c6d0ed6c2a76bf254c5cafd3877ac0c6467c47c2f4e2e39e829bbcfb43fc3cadb
6eac519e8537e4205ddd37e30a7b548a Whether "g is used in the global attribute regular expression "Tag. RegExp property, read-only
Description provided in JavaScript 1.2, NES 3.0 and above: global is a property of an individual regular expression object
If the "g" tag is used, the value of global is true; otherwise, it is false." The g" tag specifies that the regular expression tests all possible matches.
You cannot change the value of this attribute directly, but you can call the compile method to change it. ignoreCase checks whether the regular expression uses the "i" tag
RegExp attribute, read-only Description provided in JavaScript 1.2, NES 3.0 and above:
ignoreCase is a property of individual regular expression objects.
If the "i" tag is used, it returns true, otherwise it returns false. Ignore case.
You cannot change the value of this attribute directly, but you can change it by calling the compile method. The input indicates the string to be tested for.
RegExp's attribute is static. Provided in JavaScript 1.2, NES 3.0 and above
Description: Because input is static and not a property of an individual regular expression object, you can also use RegExp.input to represent it.
If there is no exec or regular expression. The test method provides a string, and if there is a value in RegExp.input, use its value to call the method.
The script or browser can preset the input attribute if it is preset and there is no value when calling the exec or test method. Providing a string
will use the input value when calling exec or test. Input can be set by the browser in the following way:
When the text form field handler is called, input is set to the string input by the text.
When the textarea form field handler is called, input is set to the string entered in the textarea field. Note that multili
ne is also set to true to match multiple lines of text. When the select form field handler is called, input is set to the value of selected text.
When the handler of the link object is called, input is set to a string between 6a2b3936541eb5c40626a82687b7bf27 and 63505a6f727f70c8bd4066f3066dcb9d.
After the event handler is processed, the value of the input attribute is cleared. lastIndex A read/write integer property indicating where the next match should begin.
Properties of RegExp Available in JavaScript 1.2, NES 3.0 and above
Description: lastIndex is a property of individual regular expression objects. This property is only set when the "g" flag of the regular expression is used for full string matching. The following rules apply:
If lastIndex is the length of the string, regexp.test and regexp.exec fail, and lastIndex is set to 0.
If lastIndex is equal to the length of the string and the regular expression matches the empty string, the regular expression starts matching from the position of lastIndex.
If lastIndex is equal to the length of the string and the regular expression does not match the empty string, the regular expression does not match input and lastIndex is set to 0.
Otherwise, lastIndex is set to the next point of the most recent match. For example, execute the script in the following order: re = /(hi)?/g matches the empty string
re("hi") returns ["hi", "hi"], and sets lastIndex to 2
re("hi" ) returns [""], an empty array, whose element with index 0 is the matching string. In this case, the empty
string is returned because lastIndex is equal to 2 (and is still 2), and the length of "hi" is also 2. lastMatch matches the last string, $& has the same meaning.
RegExp attributes, static, read-only Available in JavaScript 1.2, NES 3.0 and above
Description: Because lastMatch is static, it is not an attribute that specifies individual regular expressions. You can also use RegExp.lastMatch. lastParen
The last bracketed string match, if any. $+ means the same thing. RegExp attribute, static, read-only
Available in JavaScript 1.2, NES 3.0 and above
Description: Because lastParen is static, it is not an attribute of an individual regular expression. You can use RegExp.lastParen to express the same meaning.
leftContext most recently matched the previous substring, $` has the same meaning. RegExp attributes, static, read-only
Available in JavaScript 1.2, NES 3.0 and above
Description: Because leftContext is static and not an attribute of a regular expression, you can use RegExp.leftContext to express the same meaning.
multiline reflects whether to match multi-line text, $* has the same meaning. Attributes of RegExp, static
Available in JavaScript 1.2, NES 3.0 and above
Description: Because multiline is static, not an attribute of an individual regular expression, the same meaning can be expressed with RegExp.multiline.
multiline is true if matching multiple lines of text is allowed, false if the search must stop on a newline.
Script or browser can set the multiline attribute. When a textarea event handler is called, multiline
is set to true. After the event handler is processed, the multiline attribute value is cleared. That is to say, if you set multiline to true, multiline will be set to false after executing any event handler. prototype
Describes the prototype of a class. You can use prototype to add properties or methods of the class as per your requirement. For information on prototypes, see the Function.prototype.Property property of RegExp. Available from JavaScript 1.1, NES 2.0 version
ECMA version ECMA-262 rightContext The last matched string on the right, $' has the same effect.
RegExp properties, static, read-only Available from JavaScript 1.2, NES 3.0 and above
Description: Because rightContext is static and not an attribute of an individual regular expression worker, you can use RegExp.rightContext to achieve the same effect.
source A read-only attribute that contains the pattern defined by the regular expression, excluding forward slashes and "g" or "i" tags. Attributes of RegExp, read-only
Available from JavaScript 1.2, NES 3.0 and above
Description: source is an attribute of an individual regular expression object. You cannot change its value directly, but you can change it by calling the compile method. test
Performs a regular expression matching search for the specified string and returns true or false. RegExp method
The syntax is provided starting from JavaScript 1.2, NES 3.0 and above: regexp.test([str])
Parameters: regexp, the name of the regular expression, which can be a variable name or a regular expression definition text string
str, required Matching string, if omitted, the value of RegExp.input will be used as parameter
Description: When you need to know whether a string can match a certain regular expression, you can use the test method (similar to the String.search method
); To get more information (but the speed will be slower), you can use the exec method (similar to the String.match method). Example: The following example shows whether the test is successful:
function testinput(re, str){
if (re.test(str)) midring = " contains ";
else midring = " does not contain ";
document. write (str + midring + re.source); } toSource
Returns the source code of a string symbolizing the object. The RegExp method provides syntax from JavaScript 1.3 or above: toSource()
Parameters: No description: the toSource method returns the following values : For the built-in RegExp object, toSource returns the following character to indicate that the source code is unavailable:
function Boolean(){ [native code] }
In the case of RegExp, toSource returns a string that symbolizes the source code. Usually this method is automatically executed by JavaScript internally. Call instead of not calling explicitly in the code.
For more information, please see Object.toSource toString returns a string describing the specified object. Methods of RegExp
ECMA version ECMA-262 is provided starting from JavaScript 1.1, NES 2.0 Syntax: toString() Parameters: None
Description: RegExp object does not consider the toString method of Object object; it does not inherit Object.toString, for RegExp object
, the toString method returns a string representing the object. For example: The following example displays the string representing the RegExp object
myExp = new RegExp("a+b+c"); alert(myExp.toString())
displays "/a+b+c/" See more : Object.toString valueOf returns the original value of a RegExp object
The RegExp method is available starting from JavaScript version 1.1 ECMA version: ECMA-262 Syntax: valueOf()
Parameters: None Description: The valueOf method of RegExp returns the RegExp object in the form of a string The original value, which is equal to RegExp.toString.
This method is usually called automatically by JavaScript rather than explicitly. Example: myExp = new RegExp("a+b+c");
alert(myExp.valueOf()) displays "/a+b+c/"
JS正则表达式大全 
正则表达式在javascript中的几个实例1
! 去除字符串两端空格的处理 

如果采用传统的方式,就要可能就要采用下面的方式了 

//清除左边空格 
function js_ltrim(deststr) 
{ 
if(deststr==null)return ""; 
var pos=0; 
var retStr=new String(deststr); 
if (retStr.lenght==0) return retStr; 
while (retStr.substring(pos,pos+1)==" ") pos++; 
retStr=retStr.substring(pos); 
return(retStr); 
} 
//清除右边空格 
function js_rtrim(deststr) 
{ 
if(deststr==null)return ""; 
var retStr=new String(deststr); 
var pos=retStr.length; 
if (pos==0) return retStr; 
while (pos && retStr.substring(pos-1,pos)==" " ) pos--; 
retStr=retStr.substring(0,pos); 
return(retStr); 
} 
//清除左边和右边空格 
function js_trim(deststr) 
{ 
if(deststr==null)return ""; 
var retStr=new String(deststr); 
var pos=retStr.length; 
if (pos==0) return retStr; 
retStr=js_ltrim(retStr); 
retStr=js_rtrim(retStr); 
return retStr; 
}

采用正则表达式,来去除两边的空格,只需以下代码 

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

一句就搞定了, 
可见正则表达式为我们节省了相当的编写代码量 

! 移动手机号的校验 
如果采用传统的校验方式至少就要完成下面三步的校验, 
(1). 是否是数字 
(2).是否是11位 
(3).数字的第三位是否是5,6,7,8,9 
如果采用正则表达式校验,只需以下代码 

function checkMobile1(form) 
{ 
if (form.mobile.value > "") 
{ 
var reg=/13[5,6,7,8,9]\d{8}/; 
if ( form.mobile.value.match(reg)== null) 
{ 
alert("请输入正确的移动手机号码!"); 
form.mobile.focus(); return false; 
} 
} 
return true; 
}

从上面的代码可以看出校验移动手机号只需定义一个var reg=/13[5,6,7,8,9]\d{8}/;模式匹配串就可以完成合法性校验了 
! URL的校验, 
条件:必须以http:// 或 https:// 开头, 端口号必须为在1-65535 之间, 以下代码完成了合法性校验 

//obj:数据对象 
//dispStr :失败提示内容显示字符串 
function checkUrlValid( obj, dispStr) 
{ 
if(obj == null) 
{ 
alert("传入对象为空"); 
return false; 
} 
var str = obj.value; 
var urlpatern0 = /^https?:\/\/.+$/i; 
if(!urlpatern0.test(str)) 
{ 
alert(dispStr+"不合法:必须以&#39;http:\/\/&#39;或&#39;https:\/\/&#39;开头!"); 
obj.focus(); 
return false; 
} 
var urlpatern2= /^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?.+$/i; 
if(!urlpatern2.test(str)) 
{ 
alert(dispStr+"端口号必须为数字且应在1-65535之间!"); 
obj.focus(); 
return false; 
} 
var urlpatern1 =/^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i; 
if(!urlpatern1.test(str)) 
{ 
alert(dispStr+"不合法,请检查!"); 
obj.focus(); 
return false; 
} 
var s = "0"; 
var t =0; 
var re = new RegExp(":\\d+","ig"); 
while((arr = re.exec(str))!=null) 
{ 
s = str.substring(RegExp.index+1,RegExp.lastIndex); 
if(s.substring(0,1)=="0") 
{ 
alert(dispStr+"端口号不能以0开头!"); 
obj.focus(); 
return false; 
} 
t = parseInt(s); 
if(t<1 || t >65535) 
{ 
alert(dispStr+"端口号必须为数字且应在1-65535之间!"); 
obj.focus(); 
return false; 
} 
} 
return true; 
}

对 url的校验,看上去有很多的代码,这是因为要给予出错提示, 否则只需var urlpatern1 =/^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9_-](\?)?)*)*$/i; 一句就可以校验出url合法性了 
正则表达式在JavaScript应用 
-------------------------------------------------------------- 
去掉字符串头尾多余的空格 
/g是全文查找所有匹配 

function String.prototype.Trim(){return this.replace(/(^\s*)|(\s*$)/g, "");} 
function String.prototype.LTrim(){return this.replace(/(^\s*)/g, "");} 
function String.prototype.RTrim(){return this.replace(/(\s*$)/g, "");}

-------------------------------------------------------------- 
应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1) 

String.prototype.len=function(){return this.replace([^\x00-\xff]/g,"aa").length;}

-------------------------------------------------------------- 
应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下: 

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

得用正则表达式从URL地址中提取文件名的javascript程序,如下结果为page1 

s="http://www.php.cn" 
s=s.replace(/(.*\/){0,}([^\.]+).*/ig,"$2") 
alert(s)

##利用正则表达式限制网页表单里的文本框输入内容: 
-------------------------------------------------------------- 
用 正则表达式限制只能输入中文:

onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,&#39;)" onbeforepaste="clipboardData.setData(&#39;text&#39;,clipboardData.getData(&#39;text&#39;).replace(/[^\u4E00-\u9FA5]/g,&#39;))"

-------------------------------------------------------------- 
用 正则表达式限制只能输入全角字符: 

onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,&#39;)" onbeforepaste="clipboardData.setData(&#39;text&#39;,clipboardData.getData(&#39;text&#39;).replace(/[^\uFF00-\uFFFF]/g,&#39;))"

-------------------------------------------------------------- 
用 正则表达式限制只能输入数字:

onkeyup="value=value.replace(/[^\d]/g,&#39;) "
onbeforepaste="clipboardData.setData(&#39;text&#39;,clipboardData.getData(&#39;text&#39;).replace(/[^\d]/g,&#39;))"

-------------------------------------------------------------- 
用 正则表达式限制只能输入数字和英文:

onkeyup="value=value.replace(/[\W]/g,&#39;) "
onbeforepaste="clipboardData.setData(&#39;text&#39;,clipboardData.getData(&#39;text&#39;).replace(/[^\d]/g,&#39;))"


用正则表达式和javascript对表单进行全面验证 
代码:8108cbe1d58f0ab0b8a10168eac341c1f5a47148e367a6035fd7a2faa965022e 
将对表单中的所有以下类型的域依次验证,所有验证是去除了前导和后缀空格的,要注意是区分大小写的。 
2、空值验证 
表单中任意域加上emptyInfo属性将对此域是否为空进行验证(可以和最大长度验证\一般验证方式同时使用)。 
无此属性视为此域允许空值。 
如:76fcbe9cf4728131adeaeefb1d19e404 
3、最大长度验证(可以和空值验证、一般验证方式同时使用): 
7090effba10d5a8d06f390b8f397dffa 
或,854d848c6aacabd3a6930a1f7cc1a264 
3、一般验证方式(不对空值做验证): 
如:2777422c368dfc08ffebbfa3b9685492 
4、标准验证(不与其它验证方式同时使用): 
全部通过6bfea39e2880ed1f176d4faec8788f49来实现,并且不需要name属性以免提交到服务器。 
4.1、合法日期验证: 
9f099c6201d78ef8ee979fbb0b7c3989注:这里也可以是9b9cce9d485fd856c764c3921a38610f18bb6ffaf0152bbe49cd8a3620346341,以下同 
b4795830b8b4fd6fe33ae8fd2d234040 
df4fff216f85eb215f7ab97dd263b85d 
3ecb2c31af449d6373db17a47a5d0b4a 
yearfieldName、monthfieldName、dayfieldName分别为年月日字段,月和日可以是两位(MM)或一位格式(M), 
此处不对每个字段分别检验(如果要检验,请在年月日三个域分别使用前面的一般验证方式),只对日期的最大值是否合法检查; 
4.2、日期格式验证(请注意,此验证不对日期是否有效进行验证,还未找到从格式中得到年月日数据的方法^_^): 
e39bbdac53c9b96d654d7948d309f01f 
638eebff72278424b873f6783e9246c5 
其中格式仅对y、M、d、H、m、s进行支持(其它字符视为非时间的字符) 
4.3、列表验证: 
检验列表(checkbox、redio、select)是否至少选中了一条记录(对select主要用于多项选择) 
ecea8bd3ab8f002a9c08f722f3de1b48 
f82e4b74eb30432400fb7d6aad45b16a 
其中validatorType可以是Checkbox、R、Select; 
对于一个select表单,如果要求选择一条不能是第一条的记录,请用下列方式: 
aa3674743b1815f1e225c0b86905bf0a 
deb802d11e8b0cc341f68c280f65dae7==请选择==4afa15d3069109ac30911f04c56f3338 
22d7eb427e1a7079cd01008d37a3299c14afa15d3069109ac30911f04c56f3338 
221f08282418e2996498697df914ce4e 
4.4、Email验证: 
596bff45a69e9abd8468b4bbd281de7b 
3742f6d40f21f1d20e29773a96316594 
其中separator为可选项,表示输入多个email时的分隔符(无此选项只能是一个地址) 
4.5、加入其它javascript操作: 
5cd6e472395e766622bc5d31b556eb7a 
function functionname(){ 
自定义方法 

2cacc6d41bbb37262a98f745aa00fbf0 
表单中加入3f7460748f8d32895f1ba4c9cbc19d31(此时emptyInfo等属性无效) 
时将调用function属性中指定的javascript方法(要求方法返回true或false,返回false将不再验证表单,也不提交表单)。 
5、在表单通过验证提交前disable一个按钮(也可将其它域disable,不能与其它验证同在一个域),不要求按钮是表单中的最后一个 
065ed6893513df288c1961d1c785b207 
6、不验证表单 
a18c8eb901ff34e0ea4ac896a352fbf5 
当validator域值为0时不对表单进行验证,直接提交表单或执行指定function并返回true后提交表单 
functionName为可选 
--> 

<script type="text/javascript"> 
function getStringLength(str){ 
var endvalue=0; 
var sourcestr=new String(str); 
var tempstr; 
for (var strposition = 0; strposition < sourcestr.length; strposition ++) { 
tempstr=sourcestr.charAt(strposition); 
if (tempstr.charCodeAt(0)>255 || tempstr.charCodeAt(0)<0) { 
endvalue=endvalue+2; 
} else { 
endvalue=endvalue+1; 
} 
} 
return(endvalue); 
} 
function trim(str){ 
if(str==null) return ""; 
if(str.length==0) return ""; 
var i=0,j=str.length-1,c; 
for(;i<str.length;i++){ 
c=str.charAt(i); 
if(c!=&#39; &#39;) break; 
} 
for(;j>-1;j--){ 
c=str.charAt(j); 
if(c!=&#39; &#39;) break; 
} 
if(i>j) return ""; 
return str.substring(i,j+1); 
} 
function validateDate(date,format,alt){ 
var time=trim(date.value); 
if(time=="") return; 
var reg=format; 
var reg=reg.replace(/yyyy/,"[0-9]{4}"); 
var reg=reg.replace(/yy/,"[0-9]{2}"); 
var reg=reg.replace(/MM/,"((0[1-9])|1[0-2])"); 
var reg=reg.replace(/M/,"(([1-9])|1[0-2])"); 
var reg=reg.replace(/dd/,"((0[1-9])|([1-2][0-9])|30|31)"); 
var reg=reg.replace(/d/,"([1-9]|[1-2][0-9]|30|31))"); 
var reg=reg.replace(/HH/,"(([0-1][0-9])|20|21|22|23)"); 
var reg=reg.replace(/H/,"([0-9]|1[0-9]|20|21|22|23)"); 
var reg=reg.replace(/mm/,"([0-5][0-9])"); 
var reg=reg.replace(/m/,"([0-9]|([1-5][0-9]))"); 
var reg=reg.replace(/ss/,"([0-5][0-9])"); 
var reg=reg.replace(/s/,"([0-9]|([1-5][0-9]))"); 
reg=new RegExp("^"+reg+"$"); 
if(reg.test(time)==false){//验证格式是否合法 
alert(alt); 
date.focus(); 
return false; 
} 
return true; 
} 
function validateDateGroup(year,month,day,alt){ 
var array=new Array(31,28,31,30,31,30,31,31,30,31,30,31); 
var y=parseInt(year.value); 
var m=parseInt(month.value); 
var d=parseInt(day.value); 
var maxday=array[m-1]; 
if(m==2){ 
if((y%4==0&&y%100!=0)||y%400==0){ 
maxday=29; 
} 
} 
if(d>maxday){ 
alert(alt); 
return false; 
} 
return true; 
} 
function validateCheckbox(obj,alt){ 
var rs=false; 
if(obj!=null){ 
if(obj.length==null){ 
return obj.checked; 
} 
for(i=0;i<obj.length;i++){ 
if(obj[i].checked==true){ 
return true; 
} 
} 
} 
alert(alt); 
return rs; 
} 
function validateRadio(obj,alt){ 
var rs=false; 
if(obj!=null){ 
if(obj.length==null){ 
return obj.checked; 
} 
for(i=0;i<obj.length;i++){ 
if(obj[i].checked==true){ 
return true; 
} 
} 
} 
alert(alt); 
return rs; 
} 
function validateSelect(obj,alt){ 
var rs=false; 
if(obj!=null){ 
for(i=0;i<obj.options.length;i++){ 
if(obj.options[i].selected==true){ 
return true; 
} 
} 
} 
alert(alt); 
return rs; 
} 
function validateEmail(email,alt,separator){ 
var mail=trim(email.value); 
if(mail=="") return; 
var em; 
var myReg = /^[_a-z0-9]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/; 
if(separator==null){ 
if(myReg.test(email.value)==false){ 
alert(alt); 
email.focus(); 
return false; 
} 
} 
else{ 
em=email.value.split(separator); 
for(i=0;i<em.length;i++){ 
em[i]=em[i].trim(); 
if(em[i].length>0&&myReg.test(em[i])==false){ 
alert(alt); 
email.focus(); 
return false; 
} 
} 
} 
return true; 
} 
function validateForm(theForm){// 若验证通过则返回true 
var disableList=new Array(); 
var field = theForm.elements; // 将表单中的所有元素放入数组 
for(var i = 0; i < field.length; i++){ 
var vali=theForm.validate; 
if(vali!=null){ 
if(vali.value=="0"){ 
var fun=vali.functionName; 
if(fun!=null){ 
return eval(fun+"()"); 
} 
else{ 
return true; 
} 
} 
} 
var empty=false; 
var value=trim(field[i].value); 
if(value.length==0){//是否空值 
empty=true; 
} 
var emptyInfo=field[i].emptyInfo;//空值验证 
if(emptyInfo!=null&&empty==true){ 
alert(emptyInfo); 
field[i].focus(); 
return false; 
} 
var lengthInfo=field[i].lengthInfo;//最大长度验证 
if(lengthInfo!=null&&getStringLength(value)>field[i].maxLength){ 
alert(lengthInfo); 
field[i].focus(); 
return false; 
} 
var validatorType=field[i].validatorType; 
if(validatorType!=null){//其它javascript 
var rs=true; 
if(validatorType=="javascript"){ 
eval("rs="+field[i].functionName+"()"); 
if(rs==false){ 
return false; 
} 
else{ 
continue; 
} 
} 
else if(validatorType=="disable"){//提交表单前disable的按钮 
disableList.length++; 
disableList[disableList.length-1]=field[i]; 
continue; 
} 
else if(validatorType=="Date"){ 
rs=validateDate(theForm.elements(field[i].fieldName),field[i].format,field[i].errorInfo); 
} 
else if(validatorType=="DateGroup"){ 
rs=validateDateGroup(theForm.elements(field[i].year),theForm.elements(field[i].month),theForm.elements(field[i].day),field[i].errorInfo);
} 
else if(validatorType=="Checkbox"){ 
rs=validateCheckbox(theForm.elements(field[i].fieldName),field[i].errorInfo); 
} 
else if(validatorType=="Radio"){ 
rs=validateRadio(theForm.elements(field[i].fieldName),field[i].errorInfo); 
} 
else if(validatorType=="Select"){ 
rs=validateSelect(theForm.elements(field[i].fieldName),field[i].errorInfo); 
} 
else if(validatorType=="Email"){ 
rs=validateEmail(theForm.elements(field[i].fieldName),field[i].errorInfo); 
} 
else{ 
alert("验证类型不被支持, fieldName: "+field[i].name); 
return false; 
} 
if(rs==false){ 
return false; 
} 
} 
else{//一般验证 
if(empty==false){ 
var v = field[i].validator; // 获取其validator属性 
if(!v) continue; // 如果该属性不存在,忽略当前元素 
var reg=new RegExp(v); 
if(reg.test(field[i].value)==false){ 
alert(field[i].errorInfo); 
field[i].focus(); 
return false; 
} 
} 
} 
} 
for(i=0;i<disableList.length;i++){ 
disableList[i].disabled=true; 
} 
return true; 
} 
</script>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn