Regular expressions in JavaScript are a concise summary

  • 2020-03-30 02:34:20
  • OfStack

Methods for defining regular expressions

There are two ways to define a regular expression: constructor definition and regular expression direct quantity definition. Such as:

var reg1 = new RegExp('d{5, 11}'); //Defined by the constructor
var reg2 = /d{5, 12}/; //  By direct quantity definition 

            Replace () : this is used to perform retrieval and replacement. Take two arguments, the first a regular expression and the second a string to replace. The regular expression makes a global substitution if the modifier g is set, otherwise only the first substring that matches is replaced. If the first parameter is not a regular expression, the string is searched instead of being converted to a regular expression. Such as:

var s = "JavaScript".replace(/java/gi, "Script"); // s =  Script Script


            Match () : its argument is a regular expression, if not a RegExp transform, which returns an array of matching results. Global matching occurs if the modifier g is set. Such as:

var d = '55 ff 33 hh 77 tt'.match(/d+/g); // d = ["55", "33", "77"]


            Split () : this method splits the calling string into an array of substrings, using the split() argument, which can also be a regular expression. Such as:

var d = '123,31,453,645'.split(','); // d = ["123", "31", "453", "645"]
var d = '21 , 123,  44,  64,  67,  3'.split(/s*,s*/); // d = ["21", "123", "44", "64", "67", "3"]

RegExp objects
            Each RegExp object has five properties. The property source is a read-only string that contains the text of the regular expression. The property global is a read-only Boolean value that indicates whether the regular expression has a modifier g. The attribute ignoreCase is a read-only Boolean value that indicates whether the regular expression has the modifier I. The property multiline is a read-only Boolean value that indicates whether the regular expression has the modifier m. The lastIndex property is a readable, writable integer that stores the starting position of the next retrieval in the entire string if the matching pattern has a g modifier.
            The RegExp object has two methods. The argument to exec() is a string that functions like match(), and the exec() method performs a regular expression on a specified string, that is, a match retrieval in a string. It returns null, if failed to find any matches found a match returns an array, and this is the first element of an array containing string match the regular expression, the rest of the element is a subexpression with parentheses matching substring, regardless of whether the regular expression a modifier g, will return to the same array. When the regular expression object called exec() has the modifier g, it sets the lastIndex property of the current regular expression object to the character position next to the matched substring. When the same regular expression calls exec() a second time, it starts to retrieve from the string indicated by the lastIndex property, and if exec() does not find any matches, it resets lastIndex to 0. Such as:

var p = /Java/g;
var text = "JavaScript is more fun than Java!"
var r;
while((r = p.exec(text)) != null) {
       console.log(r, 'lastIndex: ' + p.lastIndex);

            The other method is test(), which takes a string as an argument, checks a string with test(), and returns true if it contains a match of the regular expression or false otherwise. Such as:

var p = /java/i;
p.test('javascript'); // true

Related articles: