Skip to content
Related Articles

Related Articles

Improve Article

JavaScript Symbol.matchAll Property

  • Last Updated : 22 Apr, 2021
Geek Week

Below is the example of the Symbol.matchAll property.

Example:

JavaScript




<script>
  
// JavaScript to illustrate Symbol.matchAll property
function func() {
      
    // Regular expression
    const regExp = /[0-9]+/g;
  
    // Date
    const date = "06-03-2021";
  
    // finalResult store result of matchAll property
    const result = regExp[Symbol.matchAll](date);
  
    // Iterate on all matched elements
    console.log(Array.from(result, (x) => x[0]));
}
  
func();
</script>
  • Output:
["06", "03", "2021"]

The Symbol.matchAll property returns the regular expression that matches against a string. The String.prototype.matchAll() method calls this function. The syntax of the property is as follows:

regExp[Symbol.matchAll](str);

Arguments: It takes a string used to find matches of the regular expression against the string.



Return value: The Symbol.matchAll property returns an iterator that returns regular expression matches against the string.

 

Examples for the above function are provided below:

Example 1:

const result = /a/[Symbol.matchAll]("abcd");

In this example, the Symbol.matchAll property returns an iterator that returns regular expression /a/ matches against the string “abcd” which is stored in the result. Therefore, the matched element is [“a”].

Output:

["a"]

Example 2:

const result = /[0-9]+/g[Symbol.matchAll]("06-03-2021");

In this example, the matched element to the regular expression is 06, 03 and 2021. The regular expression [0-9] shows that the matched element must contain 0 to 9. And g means global that do the global search.



Output:

["06","03","2021"]

Example 3:

const result = /[0-9]+/g[Symbol.matchAll]
    ("India got freedom in 1947");

In this example, the matched element to the regular expression is 1947. Because the only matched element is 1947.

Output:

["1947"]

Complete code for the above function are provided below:

Program 1:

JavaScript




<script>
    function func() {
  
        // Final Result store result of matchAll property
        const result = /a/[Symbol.matchAll]("abcd");
  
        // Iterate on all matched elements
        console.log(Array.from(result, (x) => x[0]));
    }
  
    func();
</script>

Output:

["a"]

Program 2:

JavaScript




<script>
    function func() {
  
        // finalResult store result of matchAll property
        const result = /[0-9]+/g[Symbol.matchAll]("06-03-2021");
  
        // Iterate on all matched elements
        console.log(Array.from(result, (x) => x[0]));
    }
  
    func();
</script>

Output:

["06","03","2021"]

Program 3:

JavaScript




<script>
    function func() {
  
        // finalResult store result of 
        // matchAll property
        const result = /[0-9]+/g[Symbol.matchAll]
            ("India got freedom in 1947");
  
        // Iterate on all matched elements
        console.log(Array.from(result, (x) => x[0]));
    }
  
    func();
</script>

Output:

["1947"]

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :