const str = "apple orange banana apple";

/* Finds only the first matching pattern in the string
   and returns the result as an array.
   (The first element of the returned array is the matched string,
   and the following properties include index, input, and groups.) */
const pattern = /apple/;
const result = str.match(pattern);

console.log(result); // Output: ["apple", index: 0, input: "apple orange banana apple", groups: undefined]
console.log(result.length); // Output: 1
console.log(result[0]);     // Output: "apple"

/* If you want to find all matching pattern strings
   throughout the entire text,
   add the g (global search) flag to the regular expression.
   When the g flag is used, the index, input, and groups properties
   are not included in the returned array.
   Instead, each matching string is returned as a separate array element. */
const globalPattern = /apple/g;
const globalResult = str.match(globalPattern);

console.log(globalResult);        // Output: ["apple", "apple"]
console.log(globalResult.length); // Output: 2
console.log(globalResult[0]);     // Output: "apple"
console.log(globalResult[1]);     // Output: "apple"

/* Checking strings against a specific pattern
   Searching for an email address pattern */
const userInfo = "My email address is user@example.com.";
const searchEmailPattern = /\w+@\w+\.\w+/g;
const searchEmailResult = userInfo.match(searchEmailPattern);

console.log(searchEmailResult);        // Output: ["user@example.com"]
console.log(searchEmailResult.length); // Output: 1
console.log(searchEmailResult[0]);     // Output: "user@example.com"
str.match(regexp);
const str = "apple orange banana apple";
const pattern = /apple/; // without the g flag
const result = str.match(pattern);

console.log(result);
// Output: ["apple", index: 0, input: "apple orange banana apple", groups: undefined]

console.log(result.length); // Output: 1
const str = "apple orange banana apple";
const globalPattern = /apple/g; // with the g flag
const globalResult = str.match(globalPattern);

console.log(globalResult); // Output: ["apple", "apple"]
console.log(globalResult.length); // Output: 2
const str = "Hello, World!";
const result = str.match();

console.log(result); 
// Output: ["", index: 0, input: "Hello, World!", groups: undefined]

console.log(result.length); // Output: 1
const str = "Hello, World!";
const result = str.match("");

console.log(result); 
// Output: ["", index: 0, input: "Hello, World!", groups: undefined]

console.log(result.length); // Output: 1
const str = "Hello, World!";
const result = str.match(" ");

console.log(result); 
// Output: [" ", index: 6, input: "Hello, World!", groups: undefined]

console.log(result.length); // Output: 1
const str = "apple orange banana apple";
const pattern = /apple/;

const result = str.match(pattern);
console.log(result); 
// Output: ["apple", index: 0, input: "apple orange banana apple", groups: undefined]

console.log(result.length); // Output: 1
const str = "apple orange banana apple";
const pattern = /apple/g; // <= add the g (global search) flag
const result = str.match(pattern);

console.log(result); // Output: ["apple", "apple"]
console.log(result[0]);
console.log(result[1]);
console.log(result.length); // Output: 2
const str = "Apple orange banana Apple";

// without the g flag / with the i flag
const pattern1 = /apple/i;
const result1 = str.match(pattern1);
console.log(result1); 
// Output: ["Apple", index: 0, input: "Apple orange banana Apple", groups: undefined]

// with the g flag / with the i flag
const pattern2 = /apple/gi; // exactly the same as /apple/gi (flag order does not matter)
const result2 = str.match(pattern2);
console.log(result2); // Output: ["Apple", "Apple"]
const str = "Hi, World!";
const pattern = /Hello/;
const result = str.match(pattern);

console.log(result); // Output: null
const text = "For inquiries, please contact help@example.com or support@example.org.";

const emailPattern =
    /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/g;
const emailMatches = text.match(emailPattern);

console.log(emailMatches);
// Output: ["help@example.com", "support@example.org"]
const text = "For more information, visit https://www.example.com.";

const urlPattern =
    /\b(?:https?:\/\/)?(?:www\.)?[a-zA-Z0-9-]+(?:\.[a-z]{2,})+(?:\/[^\s]*)?\b/g;
const urlMatches = text.match(urlPattern);

console.log(urlMatches);
// Output: ["https://www.example.com"]
const html = '<div class="container"><p>Hello, <b>world!</b></p></div>';
const tagPattern = /<[^>]+>/g;
const tags = html.match(tagPattern);

console.log(tags);
// Output: ["<div class=\"container\">", "<p>", "<b>", "</b>", "</p>", "</div>"]
const css = ".header { color: #333; } .main-content { font-size: 16px; }";
const classPattern = /\.([a-zA-Z_\d-]+)/g;
const classes = css.match(classPattern);

console.log(classes);
// Output: [".header", ".main-content"]