Skip to content

Instantly share code, notes, and snippets.

@gdibble
Created July 24, 2021 18:40
Show Gist options
  • Save gdibble/2edc1ccbe6340f887b4ba3907e26cabc to your computer and use it in GitHub Desktop.
Save gdibble/2edc1ccbe6340f887b4ba3907e26cabc to your computer and use it in GitHub Desktop.
/** replaceJSX
* RegExp search text and replace with JSX
* @see https://stackoverflow.com/a/63647844
* @param {string} str
* @param {RegExp} find
* @param {JSX} replace
* @returns {JSX}
*/
export default function replaceJSX(str, find, replace) {
const parts = str.split(find);
const result = [];
for (let i = 0; i < parts.length; i++) {
result.push(parts[i]);
if (i < parts.length - 1) result.push(replace);
}
return result;
}
@gdibble
Copy link
Author

gdibble commented Oct 14, 2021

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