{"id":13716861,"url":"https://github.com/syntax-tree/hast-util-find-and-replace","last_synced_at":"2025-06-29T22:03:40.266Z","repository":{"id":13962145,"uuid":"75485893","full_name":"syntax-tree/hast-util-find-and-replace","owner":"syntax-tree","description":"utility to find and replace text in a hast tree","archived":false,"fork":false,"pushed_at":"2023-09-21T08:48:49.000Z","size":157,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-05-17T13:48:14.255Z","etag":null,"topics":["find","hast","hast-util","html","replace","unist","util"],"latest_commit_sha":null,"homepage":"https://unifiedjs.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/syntax-tree.png","metadata":{"funding":{"github":"unifiedjs","open_collective":"unified"},"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-12-03T16:36:16.000Z","updated_at":"2024-04-28T03:46:59.000Z","dependencies_parsed_at":"2024-01-14T22:03:39.140Z","dependency_job_id":"abe8a7b7-354b-4f19-a3fd-d7bf13f577be","html_url":"https://github.com/syntax-tree/hast-util-find-and-replace","commit_stats":{"total_commits":99,"total_committers":2,"mean_commits":49.5,"dds":"0.010101010101010055","last_synced_commit":"2297027816f4ad47a686b479ae391b8053f933cc"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/syntax-tree/hast-util-find-and-replace","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-find-and-replace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-find-and-replace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-find-and-replace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-find-and-replace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/hast-util-find-and-replace/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-find-and-replace/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262175427,"owners_count":23270437,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["find","hast","hast-util","html","replace","unist","util"],"created_at":"2024-08-03T00:01:15.154Z","updated_at":"2025-06-29T22:03:40.247Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","readme":"# hast-util-find-and-replace\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n[![Sponsors][sponsors-badge]][collective]\n[![Backers][backers-badge]][collective]\n[![Chat][chat-badge]][chat]\n\n[hast][] utility to find and replace things.\n\n## Contents\n\n*   [What is this?](#what-is-this)\n*   [When should I use this?](#when-should-i-use-this)\n*   [Install](#install)\n*   [Use](#use)\n*   [API](#api)\n    *   [`defaultIgnore`](#defaultignore)\n    *   [`findAndReplace(tree, list[, options])`](#findandreplacetree-list-options)\n    *   [`Find`](#find)\n    *   [`FindAndReplaceList`](#findandreplacelist)\n    *   [`FindAndReplaceTuple`](#findandreplacetuple)\n    *   [`Options`](#options)\n    *   [`RegExpMatchObject`](#regexpmatchobject)\n    *   [`Replace`](#replace)\n    *   [`ReplaceFunction`](#replacefunction)\n*   [Types](#types)\n*   [Compatibility](#compatibility)\n*   [Security](#security)\n*   [Related](#related)\n*   [Contribute](#contribute)\n*   [License](#license)\n\n## What is this?\n\nThis package is a utility that lets you find patterns (`string`, `RegExp`) in\ntext and replace them with nodes (such as elements).\nIt’s aware of HTML (such as ignoring `\u003cstyle\u003e` and `\u003cscript\u003e` by default).\n\n## When should I use this?\n\nThis utility is typically useful when you have regexes and want to modify hast.\nOne example is when you have some form of “mentions” (such as\n`/@([a-z][_a-z0-9])\\b/gi`) and want to create links to persons from them.\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install hast-util-find-and-replace\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {findAndReplace} from 'https://esm.sh/hast-util-find-and-replace@5'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {findAndReplace} from 'https://esm.sh/hast-util-find-and-replace@5?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\n```js\nimport {h} from 'hastscript'\nimport {findAndReplace} from 'hast-util-find-and-replace'\nimport {inspect} from 'unist-util-inspect'\n\nconst tree = h('p', [\n  'Some ',\n  h('em', 'emphasis'),\n  ', ',\n  h('strong', 'importance'),\n  ', and ',\n  h('code', 'code'),\n  '.'\n])\n\nfindAndReplace(tree, [\n  [/and/gi, 'or'],\n  [/emphasis/gi, 'em'],\n  [/importance/gi, 'strong'],\n  [\n    /code/gi,\n    function ($0) {\n      return h('a', {href: '//example.com#' + $0}, $0)\n    }\n  ]\n])\n\nconsole.log(inspect(tree))\n```\n\nYields:\n\n```txt\nelement\u003cp\u003e[9]\n│ properties: {}\n├─0 text \"Some \"\n├─1 element\u003cem\u003e[1]\n│   │ properties: {}\n│   └─0 text \"em\"\n├─2 text \", \"\n├─3 element\u003cstrong\u003e[1]\n│   │ properties: {}\n│   └─0 text \"strong\"\n├─4 text \", \"\n├─5 text \"or\"\n├─6 text \" \"\n├─7 element\u003ccode\u003e[1]\n│   │ properties: {}\n│   └─0 element\u003ca\u003e[1]\n│       │ properties: {\"href\":\"//example.com#code\"}\n│       └─0 text \"code\"\n└─8 text \".\"\n```\n\n## API\n\nThis package exports the identifiers [`defaultIgnore`][api-default-ignore] and\n[`findAndReplace`][api-find-and-replace].\nThere is no default export.\n\n### `defaultIgnore`\n\nDefault tag names to ignore (`Array\u003cstring\u003e`).\n\nThe defaults are `math`, `script`, `style`, `svg`, and `title`.\n\n### `findAndReplace(tree, list[, options])`\n\nFind patterns in a tree and replace them.\n\nThe algorithm searches the tree in *[preorder][]* for complete values in\n[`Text`][text] nodes.\nPartial matches are not supported.\n\n###### Parameters\n\n*   `tree` ([`Node`][node])\n    — tree to change\n*   `list` ([`FindAndReplaceList`][api-find-and-replace-list] or\n    [`FindAndReplaceTuple`][api-find-and-replace-tuple])\n    — one or more find-and-replace pairs\n*   `options` ([`Options`][api-options])\n    — configuration\n\n###### Returns\n\nNothing (`undefined`).\n\n### `Find`\n\nPattern to find (TypeScript type).\n\nStrings are escaped and then turned into global expressions.\n\n###### Type\n\n```ts\ntype Find = RegExp | string\n```\n\n### `FindAndReplaceList`\n\nSeveral find and replaces, in array form (TypeScript type).\n\n###### Type\n\n```ts\ntype FindAndReplaceList = Array\u003cFindAndReplaceTuple\u003e\n```\n\nSee [`FindAndReplaceTuple`][api-find-and-replace-tuple].\n\n### `FindAndReplaceTuple`\n\nFind and replace in tuple form (TypeScript type).\n\n###### Type\n\n```ts\ntype FindAndReplaceTuple = [Find, Replace?]\n```\n\nSee [`Find`][api-find] and [`Replace`][api-replace].\n\n### `Options`\n\nConfiguration (TypeScript type).\n\n###### Fields\n\n*   `ignore` ([`Test`][test], optional)\n    — test for which elements to ignore\n\n### `RegExpMatchObject`\n\nInfo on the match (TypeScript type).\n\n###### Fields\n\n*   `index` (`number`)\n    — the index of the search at which the result was found\n*   `input` (`string`)\n    — a copy of the search string in the text node\n*   `stack` ([`Array\u003cNode\u003e`][node])\n    — all ancestors of the text node, where the last node is the text itself\n\n### `Replace`\n\nThing to replace with (TypeScript type).\n\n###### Type\n\n```ts\ntype Replace = ReplaceFunction | string | null | undefined\n```\n\nSee [`ReplaceFunction`][api-replace-function].\n\n### `ReplaceFunction`\n\nCallback called when a search matches (TypeScript type).\n\n###### Parameters\n\nThe parameters are the result of corresponding search expression:\n\n*   `value` (`string`)\n    — whole match\n*   `...capture` (`Array\u003cstring\u003e`)\n    — matches from regex capture groups\n*   `match` ([`RegExpMatchObject`][api-regexp-match-object])\n    — info on the match\n\n###### Returns\n\nThing to replace with:\n\n*   when `null`, `undefined`, `''`, remove the match\n*   …or when `false`, do not replace at all\n*   …or when `string`, replace with a text node of that value\n*   …or when `Array\u003cNode\u003e` or `Node`, replace with those nodes\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional types [`Find`][api-find],\n[`FindAndReplaceList`][api-find-and-replace-list],\n[`FindAndReplaceTuple`][api-find-and-replace-tuple],\n[`Options`][api-options],\n[`RegExpMatchObject`][api-regexp-match-object],\n[`Replace`][api-replace], and\n[`ReplaceFunction`][api-replace-function].\n\n## Compatibility\n\nProjects maintained by the unified collective are compatible with maintained\nversions of Node.js.\n\nWhen we cut a new major release, we drop support for unmaintained versions of\nNode.\nThis means we try to keep the current release line,\n`hast-util-find-and-replace@^5`, compatible with Node.js 16.\n\n## Security\n\nUse of `hast-util-find-and-replace` can open you up to a\n[cross-site scripting (XSS)][xss] attack if a value used to `replace` is unsafe.\nUse [`hast-util-santize`][hast-util-sanitize] to make the hast tree safe.\n\nThe following example shows how a script is injected that runs when loaded in a\nbrowser.\n\n```js\nconst tree = h('p', 'This and that.')\n\nfindAndReplace(tree, 'and', function () {\n  return h('script', 'alert(1)')\n})\n```\n\nYields:\n\n```html\n\u003cp\u003eThis \u003cscript\u003ealert(1)\u003c/script\u003e that.\u003c/p\u003e\n```\n\n## Related\n\n*   [`hast-util-select`](https://github.com/syntax-tree/hast-util-select)\n    — `querySelector`, `querySelectorAll`, and `matches`\n*   [`mdast-util-find-and-replace`](https://github.com/syntax-tree/mdast-util-find-and-replace)\n    — find and replace in mdast\n*   [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)\n    — select unist nodes with CSS-like selectors\n\n## Contribute\n\nSee [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for\nways to get started.\nSee [`support.md`][support] for ways to get help.\n\nThis project has a [code of conduct][coc].\nBy interacting with this repository, organisation, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n\u003c!-- Definition --\u003e\n\n[build-badge]: https://github.com/syntax-tree/hast-util-find-and-replace/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/hast-util-find-and-replace/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/hast-util-find-and-replace.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/hast-util-find-and-replace\n\n[downloads-badge]: https://img.shields.io/npm/dm/hast-util-find-and-replace.svg\n\n[downloads]: https://www.npmjs.com/package/hast-util-find-and-replace\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size\u0026query=$.size.compressedSize\u0026url=https://deno.bundlejs.com/?q=hast-util-find-and-replace\n\n[size]: https://bundlejs.com/?q=hast-util-find-and-replace\n\n[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg\n\n[backers-badge]: https://opencollective.com/unified/backers/badge.svg\n\n[collective]: https://opencollective.com/unified\n\n[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg\n\n[chat]: https://github.com/syntax-tree/unist/discussions\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[typescript]: https://www.typescriptlang.org\n\n[license]: license\n\n[author]: https://wooorm.com\n\n[health]: https://github.com/syntax-tree/.github\n\n[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md\n\n[support]: https://github.com/syntax-tree/.github/blob/main/support.md\n\n[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md\n\n[hast]: https://github.com/syntax-tree/hast\n\n[node]: https://github.com/syntax-tree/hast#ndoes\n\n[preorder]: https://github.com/syntax-tree/unist#preorder\n\n[text]: https://github.com/syntax-tree/hast#text\n\n[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting\n\n[hast-util-sanitize]: https://github.com/syntax-tree/hast-util-sanitize\n\n[test]: https://github.com/syntax-tree/hast-util-is-element#test\n\n[api-default-ignore]: #defaultignore\n\n[api-find-and-replace]: #findandreplacetree-list-options\n\n[api-options]: #options\n\n[api-find]: #find\n\n[api-replace]: #replace\n\n[api-replace-function]: #replacefunction\n\n[api-find-and-replace-list]: #findandreplacelist\n\n[api-find-and-replace-tuple]: #findandreplacetuple\n\n[api-regexp-match-object]: #regexpmatchobject\n","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified"],"categories":["hast utilities"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fhast-util-find-and-replace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Fhast-util-find-and-replace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fhast-util-find-and-replace/lists"}