Home >Web Front-end >JS Tutorial >Regular expressions in js (1)

Regular expressions in js (1)

伊谢尔伦
伊谢尔伦Original
2016-11-22 14:34:18972browse

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?

Regular expression is an object that describes a character pattern.

JavaScript's RegExp object and String object define methods for using regular expressions to perform powerful pattern matching and text retrieval and replacement functions.


In JavaScript, a regular expression is represented by a RegExp object. Of course , a RegExp object can be created using a RegExp() constructor, or a RegExp object can be created using a special syntax newly added in JavaScript 1.2. Just like a string literal is defined as a character enclosed in quotes,

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$");

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 character patterns.

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.

The first special character "s" matches itself literally. The second one The character "$" is a special character, which matches 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 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

_______________________________________________

If you want to use special punctuation marks in regular expressions, you must add a "" before them.

2. Character class

Put individual direct characters into square brackets It can be combined into a character class. A character class matches any character it contains, so the regular expression / [abc] / and any one of the letters "a", "b", "c"

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]/.

Since 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.

Regular gray-style character class

Character matching

__________________________________________________________
[...] 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)
______________________________________________________________

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 yet. Used to describe a number with any number of digits or a

string. The string consists of three characters and a digit following the letter. These complex patterns use regular expression syntax to specify the expression. The number of times each element is to be repeated.

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 specifically used to represent them. For example: + sign What is matched is a pattern that copies the previous pattern once

or multiple 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 .

/[^"] * / // Match zero or more non-quote characters.

Copy characters of regular expressions

Character meaning

______________________________________________________________
{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,}
____________________________________________________________________________

4. Selection, grouping and referencing

The syntax of regular expressions also includes special characters for specifying selections, grouping subexpressions and referencing previous subexpressions. The character | is used to separate the 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 group individual items into subexpressions so that they can be processed as if they were an independent unit. Use *, + or ? to process those items. For example: /java(script) ?/ matches the string "java", followed by "script" or not. /

(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 sub-patterns within the complete schema. When a regular expression successfully matches the target string, it can be extracted from the target string to match the subpattern in parentheses

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/

1 matches the pattern matched by the subexpression of the first bracket. In this example, it implements a convention that the opening quotation mark must match the closing quotation mark. Note, If the backslash is followed by more digits than the


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

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