const str = "I like yellow the most~";
const replacedStr = str.replace("yellow", "blue");

console.log(replacedStr); // "I like blue the most~"
str.replace(searchValue, newValue);
const originalString = "Hello, JavaScript! JavaScript!";
const newString = originalString.replace("JavaScript", "JS");

// The matched portion is replaced with the desired string.
// Only the first occurrence is replaced.
console.log(newString); // Output: "Hello, JS! JavaScript!"

// The original string is not modified.
console.log(originalString); // Output: "Hello, JavaScript! JavaScript!"
const originalString = "I am 20 years old.";
const newString = originalString.replace(/\d+/, "**");
// \d+ matches one or more digits

// The matched portion is replaced with the desired string.
console.log(newString); // Output: "I am ** years old."

// The original string is not modified.
console.log(originalString); // Output: "I am 20 years old."
const text = "apple banana apple";

// Using the g flag
const result = text.replace(/apple/g, "orange");
console.log(result);  // Output: "orange banana orange"

// Without the g flag
const resultNotG = text.replace(/apple/, "orange");
console.log(resultNotG);  // Output: "orange banana apple"
const originalString = "Hello, JavaScript!";
const newString = originalString.replace("JavaScript", "JS");

console.log(newString); // Output: "Hello, JS!"
const originalString = "Hello, JavaScript!";
const newString = originalString.replace("JavaScript", function(match) {
    // match is the entire substring found by replace()
    return match.toUpperCase();
});

console.log(newString); // Output: "Hello, JAVASCRIPT!"
function replacer(match, p1, p2, /* …, */ pN, offset, string, groups) {
    return replacement;
}
// Example: formatting a phone number
const phoneNumber = "123-456-7890";
const re = /(\d{3})-(\d{3})-(\d{4})/;
// (\d{3}): first group, area code
// (\d{3}): second group, prefix
// (\d{4}): third group, line number

const formattedNumber = phoneNumber.replace(re, function(match, areaCode, prefix, lineNumber) {
    // match: entire matched substring (e.g., "123-456-7890")
    // areaCode: first group (e.g., "123")
    // prefix: second group (e.g., "456")
    // lineNumber: third group (e.g., "7890")
    
    return `+1 (${areaCode}) ${prefix}-${lineNumber}`;
});

console.log(formattedNumber);
// Output: "+1 (123) 456-7890"
const email = "john.doe@example.com";
const hiddenEmail = email.replace(/(?<=.{3})[^@](?=[^@]*@)/g, "*");

console.log(hiddenEmail);
// Output: "joh*************@example.com"
const htmlString = "<p>Hello, <strong>world</strong>!</p>";
const plainText = htmlString.replace(/<[^>]*>/g, "");

console.log(plainText);
// Output: "Hello, world!"
const sentence = "JavaScript is fun!";
const replacedSentence = sentence.replace("JavaScript", "TypeScript");

console.log(replacedSentence);
// Output: "TypeScript is fun!"