Explanation of JavaScript RegExp Object

  • 2021-07-16 01:15:18
  • OfStack

What is RegExp?

Regular expressions describe pattern objects of characters.

When you retrieve some text, you can use 1 mode to describe what you want to retrieve. RegExp is this model.

A simple pattern can be a single character.

More complex patterns include more characters and can be used for parsing, format checking, substitution, and so on.

You can specify the retrieval location in the string, the type of character to retrieve, and so on.

RegExp objects are objects in native JavaScript that represent regular expressions.

The method of creating an object is: var RegExp = new RegExp(pattern, attributes);

Parameter pattern specifies a regular expression rule or a string representing a regular expression pattern;

The parameter attributes is optional and represents the modifier of the matching pattern. Contains three parameters:

1. i: Performs case-insensitive matching;


<script text="text/javascript">
    var txt = 'Hello World!'
    var reg = new RegExp('hello', 'i');
    if(reg.test(txt)) {
          console.log(txt.match(reg));
    }
</script>

2. g: Perform a global match (find all matches instead of stopping after finding the first match);


<script text="text/javascript">
  var txt = 'This is just a test.'
  var reg = new RegExp('is', 'g');// Case sensitive 
  var reg01 = new RegExp('is', 'gi');// Case insensitive 
  if(reg.test(txt)) {
    console.log(txt.match(reg));//["is", "is"]
    console.log(txt.match(reg).length);//2
  }
</script>

3. m: Performs a multi-line match (if this property is not set, ^ ($) only starts to match at the beginning (end) of the entire searched string; if this property is set, ^ ($) can also start to match after "\ r" or "\ n" of the searched string).


<script type="text/javascript">
  // The following code cannot match a string "an" , although "an" The lines have been wrapped at the back, but "an" Is not the end of a string line. 
  var txt1 = 'This is an\n apple'; 
  var reg1 = /an$/;
  console.log(txt1.match(reg1));//null
  // The following code can match the string "an"
  var txt2 = 'This is an\n apple';
  var reg2 = /an$/m;
  console.log(txt2.match(reg2));//["an", index: 8, input: "This is an↵ apple"]  
  // The following is an example of the beginning of multi-line matching 
  var txt3 = 'This is an\n apple';
  var reg3 = /^\sapp/;
  console.log(txt3.match(reg3));//null
  
  var txt4 = 'This is an\n apple';
  var reg4 = /^\sapp/m;// /^ app/m
  console.log(txt4.match(reg4));//[" app", index: 11, input: "This is an↵ apple"]
</script>

The modifiers i, g, and m can be combined and used simultaneously.

The "\" of var reg 4 =/^\ sapp/in the example above in the m modifier is an escape character. If you use the constructor to create an RegExp object, you should replace the "\" in the regular expression with "\":


<script type="text/javascript">
  var txt4 = 'This is an\n apple';
  var reg4 = new RegExp('^\\sapp', 'm');
  console.log(txt4.match(reg4));//[" app", index: 11, input: "This is an↵ apple"]
</script>

RegExp Object Properties

1.global

Returns whether the regular expression has the "g" modifier;


<script type="text/javascript">
  var txt = 'This is just a test';
  var reg = new RegExp('st', 'g');
  if(reg.global) {
    console.log(txt.match(reg));//["st", "st"]
  }
</script>

2.ignoreCase

Returns whether the regular expression has the "i" modifier;

3.multiline

Returns whether the regular expression has the "m" modifier;

4.lastIndex

Marks the position of the string in which the next match begins;


<script type="text/javascript">
  var txt = 'If you love code, you should code everyday.';
  var reg = new RegExp('ou', 'g');
  var length = txt.match(reg).length;
  for(var i = 0; i < length; i++) {
    reg.test(txt);
    console.log(reg.lastIndex);
  }
</script>

5.source

Returns the text or expression of a regular expression for pattern matching. The returned text does not include the modifiers "i", "g", "m", nor does it include the delimiter used by the direct quantity of the regular expression


<script>
  var reg1 = new RegExp('yoho', 'm');
  var reg2 = new RegExp('\\w');
  var reg3 = /\w/m;
  console.log(reg1.source);//yoho
  console.log(reg2.source);//\w
  console.log(reg3.source);//\w
</script>

RegExp Object Method

1.compile

Change or recompile regular expressions (this method is not supported by S103EN browsers)

For the redefinition of regular expressions, the following methods can be implemented, so I am not very clear about the application scenario of this method


<script>
  var reg = new RegExp('ou', 'g');
  reg = /\w/;
</script>

2.exec

Retrieve the match result of regular expression in string


<script>
  var txt = 'If you love code, you should code everyday.';
  var reg = new RegExp('ou', 'g');
  var length = txt.match(reg).length;
  for(var i = 0; i < length; i++) {
    console.log(reg.exec(txt));
  }
</script>

3.test

Detect whether the string matches the matching regular expression


<script>
  var txt = 'I code everyday.';
  var reg = new RegExp('code');
  console.log(reg.test(txt));//true
</script>

Related articles: