Skip to content

Instantly share code, notes, and snippets.

@cassidoo
Last active February 18, 2024 21:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cassidoo/8b498fd8d33cdb3181ee69a5a96fdb4e to your computer and use it in GitHub Desktop.
Save cassidoo/8b498fd8d33cdb3181ee69a5a96fdb4e to your computer and use it in GitHub Desktop.
A GPT-4 prompt to write a simple JavaScript function

Prompt: Write a function in JavaScript that wraps a Unicode character in an HTML document in a <span> tag with a CSS class.


Raw GPT-4 Output:

You can use the following JavaScript function to achieve this:

function wrapUnicodeCharacter(char, cssClass) {
  // Get the entire content of the HTML document
  const content = document.body.innerHTML;

  // Create a regex to match the Unicode character
  const regex = new RegExp(char, 'gu');

  // Replace the character with a wrapped version in a <span> with the given CSS class
  const wrappedContent = content.replace(regex, `<span class="${cssClass}">${char}</span>`);

  // Update the HTML document with the wrapped content
  document.body.innerHTML = wrappedContent;
}

This function takes two arguments:

  1. char: The Unicode character you want to wrap.
  2. cssClass: The CSS class you want to apply to the <span> element that wraps the character.

Usage example:

wrapUnicodeCharacter('𝓐', 'my-custom-class');

This will wrap all instances of the Unicode character '𝓐' in a <span> with the class 'my-custom-class'. You can then style that class in your CSS as needed.

Please note that this function modifies the entire content of the document body. If you have event listeners attached to elements in the body, they might stop working after executing this function. You may need to reattach event listeners or modify the function to work with a specific part of the document or use DOM manipulation methods such as querySelector and createElement to avoid this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment