Skip to content

Instantly share code, notes, and snippets.

@gikdev
Created December 11, 2021 21:11
Show Gist options
  • Save gikdev/b239e268d74bbe5ca48a79cffd5c2a05 to your computer and use it in GitHub Desktop.
Save gikdev/b239e268d74bbe5ca48a79cffd5c2a05 to your computer and use it in GitHub Desktop.
Replacer

In The Name Of God

Replacer

Replacer is a micro JS library that replaces a variable texts with it's value.
To use:

  1. Create an array
  2. Create an array for each variable
  3. In every array, we have two strings:
  • The first string is the variable name.
    • It can be anything
  • The second string is the text you want to replace with.
    • It can be HTML plain texts
  1. Call the $_replacer() function and pass the variable as the argument.
    For example:
var replaceThis = [
  [
    "BTN",
    "button button-primary border-red color-green"
  ],
  [
    "%-Helloee-&&",
    "Hello World!, I am using replacerJS to make my HTML codes prettier"
  ]
];

// Call the function
$_replacer(replaceThis);
function $_replacer(arr) {
let elem = document.querySelector("body");
let data = elem.innerHTML;
for (var i=0; i < arr.length; i++) {
let reg = new RegExp(arr[i][0], "g");
data = data.replace(reg, arr[i][1]);
}
elem.innerHTML = data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment