Character meaning is used as a change of meaning, that is, usually the characters after "" are not interpreted according to the original meaning. For example, /b/ matches the character "b". When a backslash is added in front of b, /b/, the change of meaning is to match a Word boundaries. -or- Restoration of regular expression function characters, such as "*" matching the metacharacter before it 0 or more times, /a*/ will match a, aa, aaa, after adding "", /a*/ will Only matches "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) will match x, save x in a variable named $1...$9, x|y will match x or y{n} will match exactly n times { n,} matches more than n times {n,m} matches n-m times [xyz] character set (character set), matches any character (or metacharacter) in this set [^xyz] does not match any character in this set Any character [b] matches a backspace b matches a word boundary B matches a non-word boundary cX Here, X is a control character, /cM/ matches Ctrl-Md matches an alphanumeric character, /d/ = / [0-9]/D matches a non-alphanumeric character, /D/ = /[^0-9]/n matches a newline character r matches a carriage return character s matches a whitespace character, including n, r, f, t , v and other S match a non-whitespace character, which is equal to /[^nfrtv]/t matches a tab character v matches a straight 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", equal to [a-zA-Z0-9] W matching a character that cannot form a word, such as [W] matching the $ in "$5.98", equal to [^a-zA-Z0-9].
It is better to use re = new RegExp("pattern",["flags"])
pattern: regular expression
flags: g (search all occurrences of pattern in the full text)
i (ignore case)
m ( Multi-line search) JavaScript dynamic regular expression question, can regular expressions be generated dynamically? For example, in JavaScript:
var str = "strTemp";
To generate:
var re = /strTemp/;
If it is a character connection:
var re = "/" + str + "/" is enough
But to generate an expression, can it be implemented? How to implement it?
JavaScript's RegExp object and String object define methods for using regular expressions to perform powerful pattern matching and text retrieval and replacement functions.
Regular expression literals are also defined as characters contained between a pair of slashes (/). Therefore, JavaScript may contain the following code:
var pattern = /s$/;
This line of code creates A new RegExp object and assign it to the variable parttern. This special RegExp object matches all strings ending with the letter "s". You can also define an equivalent regular expression using RegExp(), code As follows:
var pattern = new RegExp("s$");
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) describe characters that are matched literally. In this way, the regular expression /java/ matches
all strings that contain the substring "java". Although other strings in the regular expression Characters are not matched literally, but they all have special meanings. The regular expression /s$/ contains two characters.
1. Direct character
We have discovered that all alphabetic characters and numbers in regular expressions match themselves literally. JavaScript regular expressions also support certain non-
Alphabetic characters. For example, the sequence "n" matches a literal newline character in a string. In regular expressions, many punctuation marks have special meanings. Here are these characters and their meanings:
Regular Literal character of expression
Character matching
______________________________
Alphanumeric characters themselves
f form feed
n line feed
r carriage return
t tab
v vertical tab
/ 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 ] direct quantity
{ a { literal
} a } literal
XXX ASCII code character specified by the decimal number XXX
cJ is equivalent to n
Match. In addition, you can define negative character classes, which match all characters except those characters contained within square brackets. When defining the negative character tip, use a ^ symbol as the first character from the left square bracket.
A character. The set of regular expressions is /[a-zA-z0-9]/.
matches any character except whitespace.
__________________________________________________________
[...] Any character within brackets
[^...] Any character not within brackets
. Any character except line breaks, equivalent to [^n]
w Any single character , 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 symbol, 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)
______________________________________________________________
______________________________________________________________
{n, m} matches the previous item at least n times, But it cannot exceed m times
{n, } Match the previous item n times, or multiple times
{n} Match the previous item exactly n times
? Match the previous item 0 or 1 times, that is to say, the previous item is Optional. Equivalent to {0, 1}
+ matches the previous item 1 or more times, equivalent to {1,}
* Matches the previous item 0 or more times. Equivalent to {0,}
____________________________________________________________________________
part of
. For example, assuming that the pattern we are retrieving is one or more letters followed by one or more digits, then we can use the pattern /[a-z]+d+/. But since it is assumed that what we really care about is each Match the number at the end of
, then if we put the number part of the pattern in brackets (/ [a-z] + (d+)/) , we can extract the number from any match retrieved, and then we will Another use of this parsed .
parenthesized subexpression is to allow us to refer to an earlier subexpression later in the same regular expression. This is done by adding one or more digits after the string. Implemented. The number refers to the position of the
subexpression of the bracket in the regular expression. For example: 1 refers to the subexpression of the first bracket. 3 refers to the subexpression of the third bracket. Expression. Note that since subexpressions can be nested within other subexpressions,
so its position is the position of the counted left parenthesis.
For example: in the following regular expression is specified as 2:
/ ([Jj]ava([Ss]cript)) sis s (funw*) /
The reference to the previous subexpression in the regular expression does not specify the pattern of that subexpression, but the pattern related to that subexpression Matching text. In this way, quoting is not just a shortcut to help you enter repeated parts of a regular expression, it also enforces a convention that separate parts of a string contain exactly the same Characters. For example: The following regular expression matches all characters within single quotes or double quotes. However, it requires that the starting and ending quotes match (for example, both are double quotes or both are single quotes ):
/[' "] [^ ' "]*[' "]/If the opening and closing quotation marks are required to match, we can use the following quote:/( [' "] ) [^ ' "] * 1/
bracket subexpression, then it will be parsed as a decimal escape sequence rather than a reference. You can stick to the full three-character representation Escape sequences, which can avoid confusion. For example,
use
有关正则表达式的语法还有最后一个元素,那就是正则表达式的属性,它说明的是高级模式匹配的规则.和其它正则表达式语法不同,属性是在 / 符号之外说明的.即它
们不出现在两个斜杠之间,而是位于第二个斜杠之后.javascript 1.2支持两个属性.属性 i 说明模式匹配应该是大小写不敏感的.属性 g 说明模式匹配应该是全局的.也
就是说,应该找出被检索的字符串中所有的匹配.这两种属性联合起来就可以执行一个全局的,大小写不敏感的匹配.
例如: 要执行一个大小不敏感的检索以找到词语 "java" (或者是 "java" 、"JAVA"等) 的第一个具体值,我们可以使用大小不敏感的正则表达式 /\b java\b/i .如果要在
一个字符串中找到 "java" 所有的具体值,我们还可以添加属性 g, 即 /\b java \b/gi .
以下是正则表达式的属性:
字符 含义
_________________________________________
i 执行大小写不敏感的匹配
g 执行一个全局的匹配,简而言之,就是找到所有的匹配,而不是在找到第一个之后就停止了
_________________________________________
除属性 g 和 i 之外,正则表达式就没有其它像属性一样的特性了.如果将构造函数 RegExp 的静态属性 multiline 设置为 true ,那么模式匹配将以多行的模式进行.在这
种模式下,锚字符 ^ 和 $ 匹配的不只是检索字符串的开头和结尾,还匹配检索字符串内部的一行的开头和结尾.例如: 模式 /Java$/ 匹配的是 "Java",但是并不匹配
"Java\nis fun" .如果我们设置了 multiline 属性,那么后者也将被匹配:
RegExp.multiline = true;在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;
}[/RED]
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*)?$" //ur