Simple Text Parser is a TypeScript-based utility for synchronously parsing plain text strings using highly customizable rules defined by regular expressions or substring matches. It allows developers to transform text by replacing matches with custom HTML or by converting text into a structured tree of nodes, enabling more complex data extraction and manipulation. Currently at version 2.1.1, the library maintains a stable release cadence. Its key differentiators include its simplicity, synchronous operation for predictable performance, and the ability to define highly granular parsing rules using standard JavaScript regular expressions, making it suitable for both browser and Node.js environments. The library ships with built-in TypeScript types, providing a robust development experience.
npm install simple-text-parserVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates creating a Parser, adding a custom regex rule for hashtags, using a built-in preset for URLs, and then processing text with both `render()` for HTML output and `toTree()` for structured data.
Ensure your `replace` function returns an object `{ type: 'yourType', text: 'replacement string', value: 'data' }` if you intend to use `toTree()` for structured data. If you only care about string replacement, returning a string is sufficient.For very large inputs or performance-critical applications, consider breaking the input text into smaller chunks and parsing them incrementally, or leverage Web Workers in browser environments to avoid blocking the main thread.
Define your parsing rules from most specific to least specific, or ensure that overlapping rules are intentionally ordered to achieve the desired parsing hierarchy. Test thoroughly with various text inputs to verify rule interaction.
You must first create an instance of the `Parser` class: `const parser = new Parser();` then call `parser.addRule(...)`.
Modify your `replace` function to return an object like `{ type: 'customType', text: 'replacement HTML', value: 'extracted data' }` for `toTree()` to generate structured nodes.Review your regular expression pattern carefully. Use online regex testers (like regex101.com) to debug your pattern against example text. Ensure appropriate flags are included, such as `/pattern/gi` for global and case-insensitive matching.
No dependency data recorded yet.