RegExp The constructor contains some properties ( These properties are considered static in other languages ). These properties apply to all regular expressions in the scope , And changes based on the last regular expression operation performed . Another peculiarity about these attributes , There are two ways to access them . let me put it another way , These attributes have a long attribute name and a short attribute name, respectively (Opera It's an exception , It doesn't support short property names ). The following table lists them RegExp Properties of the constructor .

Long attribute name Short attribute name explain
input $_ Last matching string .Opera This property is not implemented
lastMatch $& Last match . Opera This property is not implemented
lastParen $+ Last matched capture group . Opera This property is not implemented
leftContext $` input In a string lastMatch The previous text
multiline $* Boolean value , Indicates whether all expressions use multiline mode .IE and Opera This property is not implemented
rightContext input In a string lastMatch The following text

You can use these attributes from exec() or text() More specific information is extracted from the operations performed . Look at examples :

  1. var text="this has been a short summer";
  2. var pattern=/(.)hort/g;
  3. /*
  4. * Be careful :Opera I won't support it input、lastMatch、lastParen and multiline attribute
  5. *IE I won't support it multiline attribute
  6. */
  7. if(pattern.test(text))
  8. {
  9. alert(RegExp.input); // this has been a short summer
  10. alert(RegExp.leftContext); //this has been a
  11. alert(RegExp.rightContext); //summer
  12. alert(RegExp.lastMatch); //short
  13. alert(RegExp.lastParen); //s
  14. alert(RegExp.multiline); //false
  15. }

Copy code

The above code creates a pattern , After matching any string hort, And put the first character in a capture group .

RegExp The properties of the constructor return the following values :

  • input Returns the original string .
  • leftContext Property returns the word short Previous string , and rightContext And back to short String after .
  • lastMatch Property returns the string that most recently matched the entire regular expression , return short.
  • lastParen Property returns the capture group that last matched , In this case s.

As shown above , The long attribute names used in the examples can be replaced by the corresponding short attribute names . It's just , Because most of these short attribute names are not valid ECMAScript identifier , So you have to access them through the square bracket Syntax , As the following example :

  1. var text="this has been a short summer";
  2. var pattern=/(.)hort/g;
  3. /*
  4. * Be careful :Opera I won't support it input、lastMatch、lastParen and multiline attribute
  5. *IE I won't support it multiline attribute
  6. */
  7. if(pattern.test(text))
  8. {
  9. alert(RegExp.$_); // this has been a short summer
  10. alert(RegExp.["$ `"]); //this has been a     $ and ` There must be no space between them , Because of the editor, we have to use space segmentation
  11. alert(RegExp.["$·"]); //summer
  12. alert(RegExp.["$ &"]); //short     $ and & There must be no space between them , Because of the editor, we have to use space segmentation
  13. alert(RegExp.["$+"]); //s
  14. alert(RegExp.["$*"]); //false
  15. }

Copy code

In addition to the properties described above , There are as many as 9 Constructor properties for storing capture groups . The syntax for accessing these properties is RegExp.$1、RegExp.$2、...、RegExp.$9, They are used to store the first 、 the second 、...、 The ninth matching capture group . Calling exec() or text() When the method is used , These properties are automatically filled in . then , We can use them like this .

  1. var text="this has been a short summer";
  2. var pattern=/(..)or(.)/g;
  3. if(pattern.test(text))
  4. {
  5. alert(RegExp.$1);//sh
  6. alert(RegExp.$2);//t
  7. }

Copy code

Here you create a pattern with two capture groups , And tested a string with this pattern . Even if test() Method returns only one Boolean value , but RegExp Properties of the constructor $1 and $2 It will also be automatically filled by the string matching the corresponding capture group .

         notes : I'm Xiaobai , Welcome to point out the mistakes in the article ^_^~~

5.4.3 RegExp More about constructor properties

  1. 5.4.1 RegExp Instance attributes

    RegExp Each instance of has the following properties , All kinds of information about patterns can be obtained through these attributes .        1.global: Boolean value , Indicates whether... Is set g sign .        2.ignoreCase: Boolean value , Express ...

  2. [ turn ]RegExp The constructor creates a regular expression object , Used to match text to a pattern

    In this paper, from :https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp RegExp  ...

  3. Regular -RegExp() Constructors

    I wrote it last time js Regular literal quantity declaration , Today, let's talk about RegExp() Constructor declaration : var p=/cat/g;   // Literal declaration var p=new RegExp('cat','g') // Constructor declaration It expresses two things ...

  4. js Advanced regular expressions 11RegExp Properties and methods of (RegExp Properties and methods of , Namely RegExp object .( spot ) The form of what )( Regular expressions are compiled before they are executed )

    js Advanced regular expressions 11RegExp Properties and methods of (RegExp Properties and methods of , Namely RegExp object .( spot ) The form of what )( Regular expressions are compiled before they are executed ) One . summary 1. RegExp Properties and methods of , Namely RegE ...

  5. js Built in constructor property modification problem

    I'm learning js There's a problem with prototyping ,Array,Object Some properties of the built-in constructor cannot be modified , I guess it's probably because of the browser implementation . 1. modify name attribute invalid . <script type=" ...

  6. JS Regular expressions go from the beginning to the end (8)—— REGEXP Object properties

    Object properties There are several common object attributes : 1.global: Full text search or not , Default false 2.ignore case: Case sensitive or not , The default is false 3.multiline: Multi line search , The default value is fal ...

  7. Reference type -RegExp type

    JavaScript Advanced programming ( The third edition ) note - The fifth chapter -RegExp type There are two ways to define regular expressions 1. Define regular expressions in literal form : ver expression = /patterns/flags; ...

  8. javascript Type system —— Regular expressions RegExp type

    × Catalog [1] object [2] Instance attributes [3] Static attribute [4] Example method In front of the word I've already introduced javascript The basic syntax of regular expressions in .javascript Of RegExp Class represents a regular expression ,String ...

  9. JS Of reference types ——RegExp

    Preface Piggy has shared something about regular expressions before , See the portal for details , Today piggy comes to share how to js Using regular expressions . Definition ECMAScript adopt RegExp Type to support regular expressions . Use something like Perl The grammar of , Just ...

Random recommendation

  1. Java Bulk insert data (Oracle)

    // Batch addition 20000 Data takes 8 second . try {    String url = "jdbc:oracle:thin:@IP:1521:orcl"; // orcl For database SI ...

  2. DrawSVG - SVG Path animation jQuery plug-in unit

    jQuery DrawSVG Used jQuery The built-in animation engine implements SVG Path animation , Yes stroke-dasharray and stroke-dashoffset attribute .DrawSVG  It's completely ...

  3. PHP Realization “ Post it, beast ” Verification Code

    The first to see “ Post it, beast ” Verification code is posted in Baidu Bar , The owner of the bar should not dig the grave , Released the ultimate beast verification code for example : Address :http://tieba.baidu.com/p/3320323440 It can be used PHP + JavaSc ...

  4. [zt] Commonly used in the system MIPS Instructions

    Instructions function Application example LB Read a byte of data from memory into a register LB R1, 0(R2) LH Read half a word of data from memory into a register LH R1, 0(R2) LW Read a word of data from memory and send it to ...

  5. express 3.0.x Does not support layout.ejs Solutions for

    1. The first method uses include use <% include Template name ( Not to add .ejs) %> Writing , As follows <% include header %> // Split the layout into two parts hea ...

  6. MVC Simple Paging

    Yes Car Car watch paging Implement simple paging , Put it here for review , There's something wrong with customizing a few items per page , To be perfected ······ 1. newly build mvc project 2. add to linq to sql Database connection 3. add to CarBF class using ...

  7. FFmpeg Of Android Platform migration — The compilation part

    Abstract : This article mainly introduces will FFmpeg Audio and video codec library transplanted to Android Compilation and basic testing process on the platform . Environmental preparation : ubuntu-12.04.5 android-ndk64-r10-linux-x86_64. ...

  8. 201521123004 《Java Programming 》 The first 7 Weekly learning summary

    1. This week's learning summary In the way you like ( Mind mapping or something ) Summarize the relevant contents of the collection . 2. Written work ArrayList The code analysis 1.1 explain ArrayList Of contains Source code answer : from ArrayList ...

  9. Some people look beautiful , Behind my back for How to write the cycle to Baidu

    Code address https://github.com/ljshLLW/homework subject The largest contiguous subarray and ( Maximum sub sum ) Given n It's an integer ( It could be negative ) The sequence of components a[1],a[2],a[3],-,a[n], ...

  10. WEB Test summary

    WEB Test summary :1.js file session Is there a cache , If there is no cache, there will be a lot of pressure on the server :2. After changing the page size , Whether the chart will request data from the server again :3. Is the form correct html identification ,script Script processing :(&l ...