Passing a Function to String.replace()

You can actually pass a function to String.replace() as the second argument. This gives you a lot of power to handle the regex results. It gives you a ton of flexibility when your manipulating text. What is a passing a function to string.replace() How do you make a passing a function to string.replace()? This script and codes were developed by Alex Vazquez on 18 November 2021, Thursday.

How do I make an passing a function to string.replace()?
  1. Passing a Function to String.replace() Previews
  2. Passing a Function to String.replace() HTML Codes
  3. Passing a Function to String.replace() CSS Codes
  4. Passing a Function to String.replace() JS Codes
Passing a Function to String.replace() Previews

Passing a Function to String.replace() HTML Codes

<!DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>Passing a Function to String.replace()</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>
  <div id="main">
  <h1>
    Passing a function to String.replace()
  </h1>
</div>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

    <script src="js/index.js"></script>

</body>
</html>

Passing a Function to String.replace() CSS Codes

#main {
 	font-family: Helvetical, sans-serif;
  color: #2e2e2e;
}

Passing a Function to String.replace() JS Codes

var text = "\
<link href='linktocss1.css'><link>\
<link href='linktocss2.css'><link>\
<link href='linktocss3.css'><link>\
";

var hrefs = [ ];

text.replace(/href='([^']+)'/g, function(matched_substring, $1, offset, string_being_examined) {
  	console.log(matched_substring);
    $('#main').append('<h3>' + $1 + '</h3>');
    hrefs.push($1);
  
    return $1; 
});

// result ["linktocss1.css", "linktocss2.css", "linktocss3.css"]
console.log(hrefs);
Do you want hide your ip address?Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup.