hex

Converts hex entities

Created by: hackvertor
Installed 1 times

Category: Convert

Created on: Thursday, October 3, 2024 at 6:59:20 PM

Updated on: Friday, December 20, 2024 at 8:08:04 PM

This is a built in tag
Tag arguments
[]
Code
class hex {
  encode(input) {
    return input
      .split("")
      .map(
        (chr) => "&#x" + chr.codePointAt().toString(16).padStart(2, "0") + ";",
      )
      .join("");
  }

  decode(input) {
    return input.replaceAll(/&#x[a-fA-F0-9]+;/gi, (c) =>
      String.fromCodePoint(parseInt(c.replaceAll(/[&#;x]/g, ""),16)),
    );
  }

  matches(input) {
    return /^(?:&#x[a-fA-F0-9]+;)+/.exec(input);
  }
}