How JavaScript defines regular expressions

Source: Internet
Author: User
Tags alphabetic character control characters

JavascriptRegular ExpressionsOf2method of definition: One is to call directlyRegExp (), the second is defined directly by the literal, i.e.var re =/Regular Rules/;

Jsof the regular expression2the method of definition is essentially calledRegExp ()Method

When calling the same regular code,ECMASCRIPT3and theECMAScript5is completely different in performance.

function Reg () {

var re =/\sjavascript/;

return re;

}

respectively inECMASCRIPT3and theECMAScript5called inReg ()method multiple times

InECMASCRIPT3, the same one is calledRegExpobject, inECMAScript5, the call is in a differentRegExpobject because inEXCMAScript5each time it is executed, a newRegExpObject

So inECMASCRIPT3Cause The program to be hidden, because whenever a change is made to the object in one place, all calls to this object will change.

1.Direct Volume characters

Characters are usually directly matched in regular, such as

/javascript/

Will match the character directlyJavaScript


Non-alphabetic character matching is also supported, such as:

\o    NULcharacter(\u0000)

\ tTab(\u0009)

\ nline Break(\u000a)

\vVertical Tab(\u000b)

\fpage Break(\u000c)

\ rCarriage return character(\u000d)

\xnnby hexadecimal numbernnspecifies the Latin character, for example,\x0aequivalent to\ n

\uxxxxby hexadecimal numberxxxxof the specifiedUnicodecharacters, such as\u0009equivalent to\ t

\cxcontrol characters^x,For example,\CJequivalent to line break\ n



InJSin regular expressions, there are some punctuation marks that have special meanings, and they need' \ 'to escape

^$.*+?=!:| \/()[]{}


2.character class

[...]any character in square brackets

[^...]any character that is not in square brackets

.        any character

\wanyASCIIA word that consists of characters, equivalent to[a-za-z0-9]

\w       any discomfortASCIIA word that consists of characters, equivalent to[^a-za-z0-9]

\sanyUnicodewhitespace characters

\sany non-Unicodecharacter of the whitespace characters, note\wand the\snot the same

\danyASCIIvalue, equivalent to[0-9]

\dexceptASCIIany character other than a number, equivalent to[^0-9]

[\b]Backspace Direct Volume (special case)

3.repetition (number of times)

?    0or1Times

+    1times or more

*any time

{n} nTimes

{M,n}Minimummtimes, up toNTimes

{N,} nTimes orNTimes above


The regular default is greedy match.


Such as[a+b+]if you want to matchAaabb, it does not matchABand theAaBand so on, will only matchAaabb

[a+?b+?]this will match .AaabWhy does that make a difference?

For:+?is to make the regular non-greedy match, thenbThis will only match oneB,then whyawill match3one of them? This is because pattern matching of regular expressions always looks for the first possible match in a string.


4.Options|Grouping|References

Used to separate the characters that are available for selection, such as[AB|CD],He can match bothABcan also matchCD, note: The attempt to match the selection order is left → right, so[A|ab], whenaafter the match has passed, it does not matchAB, even ifABIt's a better match.() 

1.individual items as sub-expressions/java (script)?can matchJavaScriptand theJavathat is, the expression of the parentheses part of the child, which can be performed on a sub-expression| * ?and other operations

2.in the complete pattern, you can refer to an expression that is preceded by parentheses after you define a sub-pattern/([' "]) [a-z]\1/\1Refers to the expression in the first parenthesis, so it references the[' "]

3.The latter refers to the preceding sub-expression note:/[' "][a-z]['"]/This regular means single or double quotation marks with a lowercase letter plus a single or double quotation marks, and the single and double quotes are not matched.

If you want a match, you can write this.[ ([']]) [a-z]\1]



\plus numbers can refer to expressions in the preceding parentheses

5.make a matching position (anchor point)

^matches the beginning of a string, in a multi-row search, matches the beginning of a line

$matches the end of a string, in a multi-row search, matches the end of a line

\bmatches the boundary of a word, in short, is the character\wand the\wthe position between, or located in the character\wThe position between the beginning and end of the string

\bmatch the position of a non-word boundary

(? =p)   0 Wide forward assertion that requires the next characters to bePmatches, but cannot include matchesPof those characters

(?! P0 Wide Negative forward assertion that requires the next character not to be associated with thePMatch

6.modifier

Write in regular expression literal//on the right

Iperform a case-insensitive match

Gperforms a global match, in short, finds all matches instead of stopping after the first one is found

Mmulti-line matching mode,^matches the beginning of a line and the beginning of a string,$match the end of the line and the end of the string/java$/mcan matchJava\nfunc

Note: when JavaScriptRegular ExpressionsIs the global time, each timeexec ()and theTest ()will put the current set ofLastIndexis set to the current position and is executed again from theLastIndex, so it's best to do it every time.LastIndexset to0.

How JavaScript defines regular expressions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.