character meaning
\

As a conversion, usually the character after “\ “is not interpreted in its original meaning, such as /b/ to match the character “b”, when b is preceded by a backslant bar /\b/ to match the boundary of a word. – or – Restores regular expression function characters, such as “* “matching its metacharacter 0 or more times, /a*/ will match a, AA,aaa, with “\”, /a\*/ will only match “A *”.

^ Matches an input or the beginning of a line, /^a/ matches “an a “, but not “an a”
$ Matches An input or the end of a line, /a$/ matches “An a “, but not “An a”
* Matches the preceding metacharacter 0 or more times, and /ba*/ will match b,ba,baa,baaa
+ Matches the preceding metacharacter once or more, and /ba*/ will match ba,baa,baaa
? Matches the preceding metacharacter 0 or 1 times, and /ba*/ will match b,ba
(x) Match x save x in name $1… In the $9 variable
x|y Matches x or y
{n} N exact matches
{n,} Match n times or more
{n,m} Match the n – m times
[xyz] A character set that matches any character (or metacharacter) in the set
[^xyz] Does not match any character in this collection
[\b] Matches a backspace character
\b Matches the boundaries of a word
\B Matches the non-boundary of a word
\cX Here, X is a control character, /\cM/ matches Ctrl-m
\d Matches a word character, /\d/ = /[0-9]/
\D Matches a non-alphanumeric character, /\D/ = /[^0-9]/
\n Matches a newline character
\r Matches a carriage return
\s Matches a blank character, including \n,\r,\f,\t,\v, etc
\S Matches a non-whitespace character equal to /[^\n\f\r\t\v]/
\t Matches a TAB character
\v Matches a rehash TAB character
\w Matches a alphanumeric character that can form a word, including an underscore, such as [\w] matches 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 “, is equal to [^ A-za-z0-9].

Re = new RegExp(“pattern “,[“flags “]) pattern: regular expression flags: g (full text search for all patterns) I (ignoring case) m (multi-line search)

VaScript dynamic regular expression problem

Can regular expressions be generated dynamically? For example in JavaScript: var STR = “strTemp “; To generate: var re = /strTemp/; Var re = “/ “+ STR + “/” How to do that?

 
     
     
[JAVA] Javascript regular expressions

正则表达式是一个描述字符模式的对象。 

JavaScript的RegExp对象和String对象定义了使用正则表达式来执行强大的模式匹配和文本检索与替换函数的方法. 

在JavaScript中,正则表达式是由一个RegExp对象表示的.当然,可以使用一个RegExp()构造函数来创建RegExp对象, 

也可以用JavaScript 1.2中的新添加的一个特殊语法来创建RegExp对象.就像字符串直接量被定义为包含在引号内的字符一样, 

正则表达式直接量也被定义为包含在一对斜杠(/)之间的字符.所以,JavaScript可能会包含如下的代码: 

var pattern = /s$/; 

这行代码创建一个新的RegExp对象,并将它赋给变量parttern.这个特殊的RegExp对象和所有以字母”s “结尾的字符串都匹配.用RegExp()也可以定义 

一个等价的正则表达式,代码如下: 

var pattern = new RegExp(“s$ “); 

无论是用正则表达式直接量还是用构造函数RegExp(),创建一个RegExp对象都是比较容易的.较为困难的任务是用正则表达式语法来描述字符的模式. 

JavaScript采用的是Perl语言正则表达式语法的一个相当完整的子集. 

正则表达式的模式规范是由一系列字符构成的.大多数字符(包括所有字母数字字符)描述的都是按照字面意思进行匹配的字符.这样说来,正则表达式/java/就和 

所有包含子串 “java ” 的字符串相匹配.虽然正则表达式中的其它字符不是按照字面意思进行匹配的,但它们都具有特殊的意义.正则表达式 /s$/ 包含两个字符. 第一个特殊字符 “s ” 是按照字面意思与自身相匹配.第二个字符 “$ ” 是一个特殊字符,它所匹配的是字符串的结尾.所以正则表达式 /s$/ 匹配的就是以字母 “s ” 结尾 的字符串. 

1.直接量字符 

我们已经发现了,在正则表达式中所有的字母字符和数字都是按照字面意思与自身相匹配的.JavaScript的正则表达式还通过以反斜杠(\)开头的转义序列支持某些非 

字母字符.例如,序列 “\n ” 在字符串中匹配的是一个直接量换行符.在正则表达式中,许多标点符号都有特殊的含义.下面是这些字符和它们的含义: 

正则表达式的直接量字符 

字符 匹配 ________________________________ 

字母数字字符 自身 \ f 换页符 \ n 换行符 \ r 回车 \ t 制表符 \ v 垂直制表符 \ / 一个 / 直接量 \ \ 一个 \ 直接量 \ . 一个 . 直接量 \ * 一个 * 直接量 \ + 一个 + 直接量 

\ ? 一个 ? 直接量 \ | 一个 | 直接量 \ ( 一个 ( 直接量 \ ) 一个 ) 直接量 \ [ 一个 [ 直接量 \ ] 一个 ] 直接量 \ { 一个 { 直接量 \ } 一个 } 直接量 \ XXX 由十进制数 XXX 指 定的ASCII码字符 \ Xnn 由十六进制数 nn 指定的ASCII码字符 \ cX 控制字符^X. 例如, \cI等价于 \t, \cJ等价于 \n 

___________________________________________________ 

如果想在正则表达式中使用特殊的标点符号,必须在它们之前加上一个 “\ ” . 

2.字符类 

将单独的直接符放进中括号内就可以组合成字符类.一个字符类和它所包含的任何一个字符都匹配,所以正则表达式 / [abc] / 和字母 “a ” , “b ” , “c ” 中的任何一个 都匹配.另外还可以定义否定字符类,这些类匹配的是除那些包含在中括号之内的字符外的所有字符.定义否定字符尖时,要将一个 ^ 符号作为从左中括号算起的第 一个字符.正则表达式的集合是 / [a-zA-z0-9] / . 

由于某些字符类非常常用,所以JavaScript的正则表达式语法包含一些特殊字符和转义序列来表示这些常用的类.例如, \s 匹配的是空格符,制表符和其它空白符, \s 匹配的则是空白符之外的任何字符. 

正则表灰式的字符类 

字符 匹配 

____________________________________________________ […] 位于括号之内的任意字符 

[^…] 不在括号之中的任意字符 . 除了换行符之外的任意字符,等价于[^\n] \w 任何单字字符, 等价于[a-zA-Z0-9] 

\W 任何非单字字符,等价于[^a-zA-Z0-9] \s 任何空白符,等价于[\ t \ n \ r \ f \ v] \S 任何非空白符,等价于[^\ t \ n \ r \ f \ v] \d 任何数字,等价于[0-9] \D 除了数字之外的任何字符,等价于[^0-9] [\b] 一个退格直接量(特例) 

________________________________________________________________ 

3.复制 

用以上的正则表式的语法,可以把两位数描述成 / \ d \ d /,把四位数描述成 / \d \ d \ d \ d /.但我们还没有一种方法可以用来描述具有任意多数位的数字或者是一个 

字符串.这个串由三个字符以及跟随在字母之后的一位数字构成.这些复杂的模式使用的正则表达式语法指定了该表达式中每个元素要重复出现的次数. 

指定复制的字符总是出现在它们所作用的模式后面.由于某种复制类型相当常用.所以有一些特殊的字符专门用于表示它们.例如: +号匹配的就是复制前一模式一次 

或多次的模式.下面的表列出了复制语法.先看一个例子: 

/\d{2, 4}/ //匹配2到4间的数字. 

/\w{3} \d?/ //匹配三个单字字符和一个任意的数字. 

/\s+java\s+/ //匹配字符串”java ” ,并且该串前后可以有一个或多个空格. 

/[^”] * / //匹配零个或多个非引号字符. 



正则表达式的复制字符 

字符 含义 

__________________________________________________________________ {n, m} 匹配前一项至少n次,但是不能超过m次 {n, } 匹配前一项n次,或者多次 {n} 匹配前一项恰好n次 ? 匹配前一项0次或1次,也就是说前一项是可选的. 等价于 {0, 1} + 匹配前一项1次或多次,等价于{1,} * 匹配前一项0次或多次.等价于{0,} 

___________________________________________________________________ 



4.选择,分组和引用 

正则表达式的语法还包括指定选择项,对子表达式分组和引用前一子表达式的特殊字符.字符| 用于分隔供选择的字符.例如: /ab|cd|ef/ 匹配的是字符串 “ab”,或者是 

字符串 “cd”,又或者 “ef”. /\d{3}|[a-z]{4}/ 匹配的是要么是一个三位数,要么是四个小写字母.在正则表达式中括号具有几种作用.它的主要作用是把单独的项目分组 

成子表达式,以便可以像处理一个独立的单元那种用 *、+或? 来处理那些项目.例如: /java(script) ?/ 匹配的是字符串 “java”,其后既可以有 “script”,也可以没有. / 

(ab|cd) + |ef) / 匹配的既可以是字符串 “ef”,也可以是字符串”ab” 或者 “cd” 的一次或多次重复. 

在正则表达式中,括号的第二个用途是在完整的模式中定义子模式。当一个正则表达式成功地和目标字符串相匹配时,可以从目标串中抽出和括号中的子模式相匹配 

的部分.例如,假定我们正在检索的模式是一个或多个字母后面跟随一位或多位数字,那么我们可以使用模式 / [a-z] + \ d+/.但是由于假定我们真正关心的是每个匹配 

尾部的数字,那么如果我们将模式的数字部分放在括号中 (/ [a-z] + (\d+)/) ,我们就可以从所检索到的任何匹配中抽取数字了,之后我们会对此进行解析的. 

代括号的子表达式的另一个用途是,允许我们在同一正则表达式的后面引用前面的子表达式.这是通过在字符串 \ 后加一位或多位数字来实现的.数字指的是代括号的 

子表达式在正则表达式中的位置.例如: \1 引用的是第一个代括号的子表达式. \3 引用的是第三个代括号的子表达式.注意,由于子表达式可以嵌套在其它子表达式中, 

所以它的位置是被计数的左括号的位置. 

例如:在下面的正则表达式被指定为 \2: /([Jj]ava([Ss]cript)) \sis \s (fun\w*) / 



对正则表达式中前一子表达式的引用所指定的并不是那个子表达式的模式,而是与那个模式相匹配的文本.这样,引用就不只是帮助你输入正则表达式的重复部分的快 

捷方式了,它还实施了一条规约,那就是一个字符串各个分离的部分包含的是完全相同的字符.例如:下面的正则表达式匹配的就是位于单引号或双引号之内的所有字 

符.但是,它要求开始和结束的引号匹配(例如两个都是双引号或者都是单引号): /[‘ “] [^ ‘ “]*[‘ “]/ 



如果要求开始和结束的引号匹配,我们可以使用如下的引用: /( [‘ “] ) [^ ‘ “] * \1/ 



\1匹配的是第一个代括号的子表达式所匹配的模式.在这个例子中,它实施了一种规约,那就是开始的引号必须和结束的引号相匹配.注意,如果反斜杠后跟随的数字比 

代括号的子表达式数多,那么它就会被解析为一个十进制的转义序列,而不是一个引用.你可以坚持使用完整的三个字符来表示转义序列,这们就可以避免混淆了.例如, 

使用 \044,而不是\44.下面是正则表达式的选择、分组和引用字符: 

字符 含义 

____________________________________________________________________ | 选择.匹配的要么是该符号左边的子表达式,要么它右边的子表达式 (…) 分组.将几个项目分为一个单元.这个单元可由 *、+、?和|等符号使用,而且还可以记住和这个组匹配的字符以供此后引 

用使用 \n 和第n个分组所匹配的字符相匹配.分组是括号中的子表达式(可能是嵌套的).分组号是从左到右计数的左括号数 

____________________________________________________________________ 



5.指定匹配的位置 

我们已经看到了,一个正则表达式中的许多元素才能够匹配字符串的一个字符.例如: \s 匹配的只是一个空白符.还有一些正则表达式的元素匹配的是字符之间宽度为 

0的空间,而不是实际的字符例如: \b 匹配的是一个词语的边界,也就是处于一个/w字字符和一个\w非字字符之间的边界.像\b 这样的字符并不指定任何一个匹配了的 

字符串中的字符,它们指定的是匹配所发生的合法位置.有时我们称这些元素为正则表达式的锚.因为它们将模式定位在检索字符串中的一个特定位置.最常用的锚元 

素是 ^, 它使模式依赖于字符串的开头,而锚元素$则使模式定位在字符串的末尾. 

例如:要匹配词 “javascript” ,我们可以使用正则表达式 /^ javascript $/. 如果我们想检索 “java” 这个词自身 (不像在 “javascript” 中那样作为前缀),那么我们可以使 

用模式 /\s java \s /, 它要求在词语java之前和之后都有空格.但是这样作有两个问题.第一: 如果 “java” 出现在一个字符的开头或者是结尾.该模式就不会与之匹配,除 

非在开头和结尾处有一个空格. 第二: 当这个模式找到一个与之匹配的字符时,它返回的匹配的字符串前端和后端都有空格,这并不是我们想要的.因此,我们使用词语 

的边界 \b 来代替真正的空格符 \s 进行匹配. 结果表达式是 /\b java \b/. 下面是正则表达式的锚字符: 

字符 含义 

____________________________________________________________________ ^ 匹配的是字符的开头,在多行检索中,匹配的是一行的开头 $ 匹配的是字符的结尾,在多行检索中,匹配的是一行的结尾 \b 匹配的是一个词语的边界.简而言之就是位于字符\w 和 \w之间的位置(注意:[\b]匹配的是退格符) \B 匹配的是非词语的边界的字符 

_____________________________________________________________________ 



6.属性 

有关正则表达式的语法还有最后一个元素,那就是正则表达式的属性,它说明的是高级模式匹配的规则.和其它正则表达式语法不同,属性是在 / 符号之外说明的.即它 

们不出现在两个斜杠之间,而是位于第二个斜杠之后.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;

Check whether a string is in the format of an email in JAVASCRIPT:

if(formname.email.value! = formname. Email. Value. The match (/ ^ \ w + / @ \ w + [.] [\ w.] + $/)) {alert (” your email format error!” ); 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]

 
17 regular expressions for javascript
 
“^ \ \ d + $” nonnegative integer / / + 0 (positive integer)” ^ [0-9] * [1-9] [0-9] * $” / / positive integer

“^ ((\ \ d +) | (0 +)) $” / / not a positive integer (negative integers + 0)” ^ – [0-9] * [1-9] [0-9] * $” / / negative integer

“^ -? \ \ d + $/ / “integer” ^ \ \ d + (\ \. \ \ d +)? $”// non-negative float (positive float + 0)

“^ (([0-9] + \ \ [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \ \ [0-9] +) | ([0-9] * [1-9] [0-9] *)) $” / / is floating point number

“^((-\\d+(\\.\\d+)?) | (0 + (\ \. 0 +)?) )$”// non-positive float (negative float + 0)

“^ (- (([0-9] + \ \ [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \ \ [0-9] +) | ([0-9] * [1-9] [0-9] *))) $” / / negative floating point number

“^ (-? \\d+)(\\.\\d+)? $”// Float number “^[A-za-z]+$”// A string of 26 English letters

“^ [a-z] + $” / / string composed of 26 English letters capitalized” ^ [a-z] + $” / / string composed of 26 English letters lowercase

“^ [A Za – z0-9] + $” / / string composed of 26 English letters and Numbers” ^ \ \ w + $/ / “by the Numbers, string composed of 26 English letters or underscores

“^ [-] \ \ w + (\ \ [-] \ \ w +) * @ [-] \ \ w + (\ \ [-] \ \ w +) + $” / / email address

“^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\? \\S*)? $” / / url
Regular expressions in JavaScript (2)
 
 
 

Properties and methods of regular expression objects Predefined regular expressions have the following static properties: Input, Multiline, lastMatch, lastParen, leftContext, rightContext, and $1 through $9. Input and multiline can be pre-set. The values of other attributes are assigned different values depending on conditions after executing exec or test methods. Many properties have both long and short (Perl-style) names, and both names point to the same value. (JavaScript emulates Perl’s regular expressions) property of a regular expression object

attribute meaning
The $1… $9 If it exists, it is the matched substring
The $_ See the input
$* See the multiline
$& See lastMatch
+ $ See lastParen
$` See leftContext
$’ ‘ See rightContext
constructor Creates a special function prototype for an object
global Matches in the whole string (bool)
ignoreCase Whether to ignore case when matching (type bool)
input The string that is matched
lastIndex The index that was last matched
lastParen The last substring enclosed in parentheses
leftContext The last match was to the left of the substring
multiline Whether to match multiple rows (type bool)
prototype Allows attributes to be attached to objects
rightContext The substring last matched to the right
source Regular expression pattern
lastIndex The index that was last matched

Methods of regular expression objects

methods meaning
compile Regular expression comparison
exec Perform a lookup
test matching
toSource Literal definitions of a particular object are returned, whose value can be used to create a new object. Override the object.tosource method.
toString Returns a string of specific objects. Override the Object.toString method.
valueOf Returns the original value of a particular object. Override the object.valueof method

Example <script language = “JavaScript”> var myReg = /(w+)s(w+)/; var str = “John Smith”;

var newstr = str.replace(myReg, “$2, $1”); document.write(newstr); 

</script> will print “Smith, John”

 

 

 

Javascript regular expression verification

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * EO_JSLib in js * javascript regular expression test * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / check whether all composed of digital function isDigit (s) { Var patrn = / ^ [0-9] {1, 20} $/; if (! Patrn. Exec (s)) return false return true} // Verify the login name. The login name can contain only 5 to 20 characters, including digits, underscores (_), and underscores (_), starting with a letter. The string function isRegisterUserName (s) {var patrn = / ^ {1} [a zA – Z] ([a zA – Z0-9] |. [_]) {4} 3 $/; if (! Patrn. Exec (s)) return false return true} Function isTrueName(s) {var patrn=/^[a-za-z]{1,30}$/; if (! Function isPasswd(s) {var patrn=/^(\w){6,20}$/; function isPasswd(s) {var patrn=/^(\w){6,20}$/; if (! Patrn. Exec (s)) return false return true} Can be “+” at the beginning, in addition to the digital, can contain a “-” function isTel (s) {/ / var patrn = / ^ [+] {0, 1} (\ d {1, 3} [])? ([-]? (\ d {1, 12}) + $/; Var patrn = / ^ [+] {0, 1} (\ d {1, 3} [])? ([-]? ((\ d) | []) {12} 1) + $/; if (! Patrn.exec (s)) return false return true} Must begin with Numbers, in addition to the digital, can contain a “-” function isMobil (s) {var patrn = / ^ [+] {0, 1} (\ d {1, 3} [])? ([-]? ((\ d) | []) {12} 1) + $/; if (! 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} / / check search keyword function isSearch (s) {var patrn = / ^ [^ ` ~! @ # $% ^ & * () + = | \ \ \] [\] \ {\} :; ‘\,. < > /? ] {1} [^ ` ~! @ $% ^ & () + = | \ \ \] [\] \ {\} :; ‘\,. < >? ] {0} 3 $/; if (! } function isIP(s) {var patrn=/^[0-9.]{1,20}$/; if (! patrn.exec(s)) return false return true }

 

 

Regular expression regular expression

Regular expression is a regular expression. Regular expressions have a very powerful and complex object, RegExp, available in JavaScript1.2. A regular expression object is used to regulate a regular expression (that is, whether the expression conforms to certain requirements, such as an Email address format), and it has properties and methods for checking whether a given string conforms to the rules. In addition, the properties of individual regular expression objects that you create with the RegExp constructor already have the static properties of regular expression objects defined in advance, and you can use them at any time. Core objects: Available in JavaScript 1.2, NES 3.0 and above. The toSource method has been added to JavaScript since version 1.3. Method: text format or RegExp constructor function. The text creation format uses the following format: /pattern/flags that is, /pattern/flags

Constructor function methods: new RegExp(“pattern”[, “flags”])

Parameter: pattern Represents the text of the regular expression

If this is specified, flags can be one of the following values: G: global match I: ignore case gi: Both global match and Ignore case(match all possible values, also ignore case)

Note: Arguments in text format should not be quoted, while arguments in constructor functions should be quoted. So the following expression creates the same regular expression: /ab+c/ I new RegExp(“ab+c”, “I “)

Description: When using constructors, it is necessary to use normal strings to avoid the rule (adding the leading character \ to the string). For example, the following two statements are equivalent: re = new RegExp(“\\w+”) re = /\w+/

The following provides a complete list and description of the complete pairs of special characters that can be used in regular expressions.

Table 1.3: Special characters in regular expressions:

Character \ meaning: For characters, usually in a literal sense, indicating that the following character is a special character, \ is not explained. For example, the /b/ matches the character ‘b’, which becomes a special character by preceded by a backslash \, that is, /\b/, indicating the boundary between matching a word. Or: For several characters, the usual description is special, indicating that the character immediately following is not special but should be interpreted literally. For example, * is a special character that matches any character (including 0 characters). For example, /a*/ indicates that zero or more AS are matched. To match the literal *, precede the a with a backslash; For example: /a\*/ matches ‘a*’.

Character ^ Meaning: Indicates that the matching character must be first. For example: /^A/ does not match the ‘A’ in “an A,” but matches the first ‘A’ in “an A.”.

The $character meaning: similar to ^, matches the last character. For example: /t$/ does not match the ‘t’ in “Eater”, but matches the ‘t’ in “eat”.

Character * Meaning: Matches the character preceded by * 0 or n times. For example :/bo*/ matches ‘boooo’ in “A Ghost Booooed “or ‘b’ in “A bird Warbled”, but does not match any characters in “A goat g runted”.

Character + Meaning: Matches the character before the + sign once or n times. Equivalent to {1,}. For example: /a+/ matches ‘a’ in “candy” and all ‘a’ in “caaaaaaandy.”

Characters? Meaning: Match? The preceding character is 0 or 1 times. For example: / e? le? / matches ‘el’ in “angel” and ‘le’ in “Angle.”

Character. Meaning :(decimal point) matches all single characters except newline. For example: /.n/ matches ‘an’ and ‘on’ in “nay, an apple is on the tree”, but does not match ‘nay’.

The character (x) meaning: Matches ‘x’ and records the matched value. For example: /(foo)/ matches and records ‘foo’ in “foo bar.” Matching substrings can be matched by elements in the result array [1]… , [n] returns, or is the property of the RegExp object $1… , $9 returns.

Character x | y: matching ‘x’ or ‘y’. For example: / green | red/match “green apple” in the ‘green’ and “‘ red ‘. In the red apple.”

Meaning of the {n} character: where n is a positive integer. Matches the first n characters. For example: /a{2}/ does not match the ‘A’ in “candy,” but matches all ‘A’ in “caandy,” and the first two ‘a’ in “caaandy.”

Meaning of the character {n,} : where n is a positive integer. Matches at least n preceding characters. For example: /a{2,} does not match ‘a’ in “candy”, but matches all ‘A’ in “caandy” and all ‘A’ in “caaaaaaandy.”

Meaning: the characters {n,m} are both positive integers. Matches at least n characters and at most m characters. For example: /a{1,3}/ does not match any characters in “cndy”, but matches the ‘a’ in “candy,” the first two ‘a’ in “caandy,” and the first three ‘a’ in “caaaaaaandy”, note: Even though “caaaaaaandy” has a lot of ‘A’s, it only matches the first three’ A’s, “AAA”.

Character [xyz] meaning: A list of characters matching any character in the list. You can indicate a character range by hyphens. For example, [abcd] is the same as [a-c]. They match the ‘B’ in “brisket” and the ‘C’ in “ache”.

Character [^xyz] meaning: a character complement, that is, it matches everything except the listed characters. You can use a hyphen – to indicate a character range. For example: [^ ABC] and [^ a – c] equivalent, ‘r’ in their first match “brisket” and “chop.” the ‘h’.

Character [\b] meaning: matches a space (not to be confused with \b)

Character \b meaning: Matches the dividing line of a word, such as a space (not to be confused with [\b]) e.g. /\bn\w/ matches ‘no’ in “noonday”, /\wy\b/ matches ‘ly’ in “possibly yesterday.”

Character \B meaning: matches the nondividing line of a word e.g. /\w\Bn/ matches the ‘on’ in “noonday”, /y\B\w/ matches the ‘ye’ in “possibly yesterday.”

Character \cX meaning: here X is a control character. Matches the control character of a string. 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 ‘2’ in “B2 is the suite number.”

Character \D meaning: matches any non-numeric, equivalent to [^0-9]. For example: /\D/ or /[^0-9]/ matches ‘B’ in “B2 is the suite number.”

Character \f meaning: Matches a form character

Character \n meaning: Matches a newline character

Character \r meaning: Matches a carriage return character

Meaning of character \s: Matches a single white space, including space, TAB, form feed, and newline, equivalent to [\f\n\r\t\v]. For example: /\s\w*/ matches’ bar’ in “foo bar.”

Character \S meaning: Matches a single character other than white space, equivalent to [^ \f\n\r\t\v]. For example: /\S/\w* matches ‘foo’ in “foo bar.”

Character \t meaning: Matches a TAB character

Character \v meaning: Matches a leading TAB character

Character \w meaning: Matches all numbers and letters as well as underscores, equivalent to [A-za-z0-9_]. For example: /\w/ matches ‘a’ in “apple,” ‘5’ in “$5.28,” and ‘3’ in “3D.”

Character \W meaning: Matches any character other than digits, letters, and underscores, equivalent to [^ A-za-z0-9_]. For example, /\W/ or /[^$a-za-z0-9_]/ matches the ‘%’ in “50%.”.

Character \n meaning: where n is a positive integer. Matches the value of n (counting left parentheses) of the last substring of a regular expression.

For example: /apple(,)\sorange\1/ matches the ‘apple, orange’ in “apple, orange, cherry, peach.” Here’s a more complete example. Note: if the number in the left parentheses is smaller than the number specified by \n, \n takes the octal escape of the next line as the description.

The meaning of the characters \ooctal and \xhex: here \ooctal is an octal escape value, while \xhex is a hexadecimal escape value that allows embedding ASCII codes in a regular expression.

Text symbols provide a way to edit regular expressions when they are checked. Regular expressions can be kept constant by means of literal symbols. For example, if you use literal symbols in a loop to construct a regular expression, the regular expression does not need to be compiled repeatedly. Regular expression object constructors, such as New RegExp(“ab+c”), provide run-time compilation of regular expressions. Constructors should be used when you know that the patterns of positive expressions change, or when you do not know the patterns of regular expressions and they are obtained from another source, such as user input. Once you have defined a regular expression that can be used anywhere and can be changed, you can compile a new regular expression to reuse using the compilation method. A separate pre-defined RegExp object can be used in each window; That is, each separate JavaScript thread runs to get its own RegExp object. Because each script is non-interruptible within a thread, this ensures that different scripts do not overwrite the value of the RegExp object. The predefined RegExp object contains static attributes: Input, Multiline, lastMatch,lastParen, leftContext, rightContext, and from $1 to $9. The input and multiline attributes can be preset. The values of other static attributes are set after executing the exec and test methods of individual regular expression objects, and after executing the match and replace methods of strings.

Properties Note that several properties of the RegExp object have both long and short names (like Perl). These names all refer to the same value. Perl is a programming language, and JavaScript mimics its regular expressions.

Properties of $1,… , $9 gets a matching substring, if any

Attribute $_ reference input

Attribute $* refer to multiline

Attribute $& refer to lastMatch

Attribute $+ refer to lastParen

The $’ attribute refers to leftContext

The $’ attribute refers to rightContext

The constructor property specifies that the object prototype function is created

The global attribute determines whether to test whether the regular expression does not match all strings, or simply conflicts with the first one.

The ignoreCase attribute determines whether case is ignored when attempting to match strings

Attribute INPUT Is the opposite string when the regular expression is matched.

The lastIndex attribute determines where the next match starts

Property lastMatch Last matched character

Attribute lastParen substring matches when the last parenthesized, if any.

The substring before the last match of the leftContext property.

Whether the property multiline is searched on multiple lines of a string.

The prototype attribute allows you to attach attributes to all objects

The substring of the rightContext property that was last matched.

Property source Schema text

 

The compile method compiles a regular expression object

The exec method runs regular expression matching

The test method tests regular access matching

The toSource method returns a literal description of the object specified; You can use this value to create a new object. Object. ToS ource methods are not considered.

The toString method returns a string describing the specified Object, regardless of object.toString.

The valueOf method returns the original valueOf the specified diagonal. The Object.valueof method is not considered.

In addition, this object inherits the object’s watch and unwatch methods

Example: Example 1. The following example script uses the replace method to convert words in a string. In the replacement text, the script uses the values of the $1 and $2 properties of the global RegExp object. Notice the name of the $property of the RegExp object when passed as the second argument to the replace method. < SCRIPT LANGUAGE = “JavaScript1.2” > re = / (\ w +) \ s +) (\ w /; str = “John Smith”; newstr=str.replace(re,”$2, $1″); Document. write(newstr) displays the result: “Smith, John”.

Example 2. In the following example script, regexp. input is set by the Change event handler. In the getInfo function, the exec method takes the value of regexp. input as its argument. Note that RegExp presets the $attribute.

Please enter your last name and age, and press Enter. <FORM><INPUT TYPE=”TEXT” NAME=”NameAge” onChange=”getInfo(this);” > < / FORM > < / HTML >

At $1,… The $9 attribute matches the substring in parentheses, if any. Is a property of RegExp static, read-only

In JavaScript 1.2, NES 3.0 and up, the description is provided: because input is a static property, it is not an attribute of an individual regular expression object. You can access this property using regexp. input.

There is no limit to the number of substrings that can be parenthetical, but regular expression objects can keep only the last nine. If you want to access all the matching strings in parentheses, you can use the returned array.

These attributes can be used to replace the string (output) with the regexp. replace method. When using this approach, you don’t have to worry about the RegExp object first. Examples are given below. When no parentheses are included in the regular expression, the script interprets $n literally. (where n is a positive integer).

For example, the following script uses the replace method to swap the positions of words in the string. In the replaced text string, the script uses the values of the $1 and $2 properties of the regular expression RegExp object. Note: When they pass arguments to the replace method, the name of the RegExp object for the $attribute is not taken into account. < SCRIPT LANGUAGE = “JavaScript1.2” > re = / (\ w +) \ s +) (\ w /; str = “John Smith”; newstr=str.replace(re,”$2, $1″); Document. write(newstr) The output is Smith, John.

 

Regular expression regular expression

Regular Expression Details (2)

For the following new objects that are not regular expressions see the corresponding JavaScript object property $_ property reference Input $* Property reference multiline $& property reference lastMatch $+ property reference lastParen $’ property reference leftContext $’ attribute reference rightContext compile method A method that compiles regular expression objects during a script run that belongs to RegExp provides syntax in JavaScript 1.2, NES 3.0 and above: Regexp.compile (pattern[, flags]) Number: regexp Specifies the name of the regular expression. The value can be a variable name or a string. The definition text of the pattern regular expression. Flags, if specified, can be one of the following: “g”: matches all possible strings. “I “: ignores case and “gi”: Matches all possible strings and ignores case description: Compile a regular expression using the compile method created with the RegExp constructor function. This forces the regular expression to be compiled once, rather than every time it is encountered. Use the compile method when you are sure that the regular expression will stay the same (after getting its match pattern), so you can use it multiple times in your script. You can also use the compile method to change regular expressions at run time. For example, if the regular expression changes, you can use the compile method to recompile the object to make it more efficient. Using this method changes the values of the source, global, and ignoreCasesource properties of the regular expression. Constructor refers to functions that prototype objects. Note that the value of this Property is provided by the function itself, not by a string containing RegExp’s name.property. The ECMA version of ECMA-262 is described in JavaScript 1.1, NES 2.0 and above: refer to the object.constructor. exec method to run a match search on the specified string. Returns an array of results. Exec ([STR]) RegExp ([STR]) Parameter: RegExp, the name of a regular expression, can be a variable name or a literal definition string. STR, the string to match the regular expression. If omitted, the value of regexp. input is used. Description: As in the syntax description, the regular expression exec method can be called directly (using regexp.exec(STR)) or indirectly (using regexp(STR)). If you’re just running to find a match, you can use the String search method. If the match is successful, the exec method returns an array and updates the value of the regular expression object property and the predefined regular expression object, RegExp. If the match fails, the exec method returns NULL. The following are the return values of the SCRIPT: Object property /Index Description Example myArray

MyArray contents [“dbBd”, “bB”, “d”] index 0 based match index 1 Input Original string cdbBdbsbz [0] Last matched character dbBd [1],… [n] Match string enclosed in parentheses, if any. There is no limit to the number of parentheses. [1] = bB [2] = d myRe lastIndex index value to start the next match 5 ignoreCase indicates whether “I” is used to ignoreCase true global indicates whether the “g” flag is used to match all possible strings true Source text string d(b+)(d) RegExp lastMatch$& last matched character dbBd leftContext$\Q last matched substring c rightContext$’ last matched substring BSBZ $1 . Matching substring in parentheses, if any. The number of parentheses is unlimited, but RegExp can keep only the last nine $1 = bB $2 = d lastParen $+ the last parenthes-matched substring, if any d

If your regular expression uses the “G” flag, you can use the exec method multiple times to match the same string consecutively. When you do this, the new match will start in the substring determined by the value of the lastIndex property of the regular expression. For example, suppose you use the following SCRIPT: