Examples of Usage of Common Regular Functions in JavaScript

  • 2021-07-13 04:19:27
  • OfStack

This paper describes the usage of regular functions commonly used in JavaScript with examples. Share it for your reference, as follows:

JavaScript regular functions match, exec, test, search, replace, split are used.

* Attributes "g", "i", and "m", which specify global, case-sensitive, and multi-row matches, respectively.

1. The match () function

match () function: If no match is found, the return value is null; If a match is found, an array of results is returned.


function RegExpMatch() {
  var str = "https://www.ofstack.com/jiaoben";
  var reg = /[j][\s\S]{3}/gi; //  Attention g Matches the full text, and if you don't add it, you will always return only the first 1 Matches 
  var result = str.match(reg);
  alert(result); // Return to: jb51,jiao
}

2. exec () Function

exec () function: If no match is found, the return value is null; If a match is found, an array of results is returned.

The 0th element of this array is the text that matches the regular expression, the first element is the text that matches the first subexpression of RegExpObject (if any), the second element is the text that matches the second subexpression of RegExpObject (if any), and so on.


function RegExpExec() {
  var str = "1234-5678";
  var reg = /(\d{4})-(\d{4})/;
  var result = reg.exec(str);
  alert(result); // Return to: 1234-5678,1234,5678
  alert(result[1] + ' ' + result[2]); //1234 5678
  alert(RegExp.$1 + ' ' + RegExp.$2); //1234 5678
}

3. test () Function

test () function: Returns an Boolean value indicating whether the given regular expression is matched in the string being looked up.


function RegExpTest() {
  var str = "https://www.ofstack.com/jiaoben";
  var reg = /^http:\/\/([\w-]+\.)+[\w-]+(\/[\w-]*)?$|^([\w-]+\.)+[\w-]+(\/[\w-]*)?$/;  // Validation URL Format 
  var result = false;
  if (reg.test(str)) {
    result = true;
  }
  alert(result);  //true
}

4. search () Function

search () function: Returns the position of the first substring that matches what the regular expression looks for, or-1 if no match is found.


function RegExpSearch() {
  var str = "https://www.ofstack.com/jiaoben";
  var reg = /(jiaoben)/;
  var result = str.search(reg);
  alert(result);  //20
}

5. replace () Function

replace () Function: Returns a copy of a string after text substitution based on a regular expression.


function RegExpReplace() {
  var str = "https://www.ofstack.com/jiaoben";
  var reg = /^(http:\/\/www.ofstack.com)\/([\w]*)$/;
  var result = str.replace(reg, "$1?userId=$2");
  alert(result);  //https://www.ofstack.com?userId=jiaoben
}

6. split () Function

split () Function: Divides a string into substrings and returns the result as an array of strings.


function RegExpSplit() {
  var str = "1@4@7@9";
  var reg = /@/;
  var result = str.split(reg); ;
  alert(result);  //[1,4,7,9]
}

PS: Here are two very convenient regular expression tools for your reference:

JavaScript Regular Expression Online Test Tool:
http://tools.ofstack.com/regex/javascript

Regular expression online generation tool:
http://tools.ofstack.com/regex/create_reg

For more readers interested in JavaScript related contents, please check out the topics of this site: "JavaScript Regular Expression Skills Encyclopedia", "JavaScript Replacement Operation Skills Summary", "JavaScript Search Algorithm Skills Summary", "JavaScript Data Structure and Algorithm Skills Summary", "JavaScript Traversal Algorithm and Skills Summary", "json Operation Skills Summary in JavaScript", "JavaScript Error and Debugging Skills Summary" and "JavaScript Mathematical Operation Usage Summary"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: