npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

simple-text-parser2

v2.0.0

Published

A dead simple, customizable plain text parser.

Readme

Simple Text Parser

This is a dead simple text parser written in Javascript. It's based around strings and regular expressions so it's highly customizable, synchronous and relatively fast.

Install

Requires Node.js and NPM. Simply install it into your package of choice.

npm install simple-text-parser --save

The --save will tell npm to add it to your package.json.

Usage

The STP package is a Parser class. Create a new object from it.

var Parser = require("simple-text-parser"),
    parser = new Parser();

Examples

STP works by taking a plain text String and searching it for substrings and regular expressions. When a match is found, it is parsed out into a tree and replaced.

Let's start by defining a parsing rule. Say we want to parse some text for hash tags (#iamahashtag) and replace it with some custom html:

// Define a rule using a regular expression
parser.addRule(/\#[\S]+/ig, function(tag) {
	// Return the tag minus the `#` and surrond with html tags
	return "<span class=\"tag\">" + tag.substr(1) + "</span>";
});

Now lets parse some text and output the resulting string:

parser.parse("Some text #iamahashtag foo bar.");

becomes...

Some text <span class="tag">iamahashtag</span> foo bar.

Of course we can also parse some text into an Object tree for more custom handling and to retrieve the parsed data:

parser.toTree("Some text #iamahashtag foo bar.");

outputs...

[ { type: 'text', text: 'Some text ' },
  { type: 'text',
    text: '<span class="tag">iamahashtag</span>' },
  { type: 'text', text: ' foo bar.' } ]

Of course a type of text on a tag isn't helpful when specifically trying to parse out tags. Let's modify our parsing rule to be more specfic:

// Define a rule using a regular expression
parser.addRule(/\#[\S]+/ig, function(tag) {
	// Get the tag minus the `#`
	var clean_tag = tag.substr(1);
	
	// create the replacement text with surronding html tags
	var text = "<span class=\"tag\">" + clean_tag + "</span>";
	
	// return an object describing this tag
	return { type: "tag", text: text, tag: clean_tag };
});

Now lets rerun parse() and toTree() on the original text. Notice that parse() outputs the same thing as before, but toTree() includes the custom meta data.

Some text <span class="tag">iamahashtag</span> foo bar.
[ { type: 'text', text: 'Some text ' },
  { type: 'tag',
    text: '<span class="tag">iamahashtag</span>',
    tag: 'iamahashtag' },
  { type: 'text', text: ' foo bar.' } ]

API Documentation

Class Methods

These methods can be called directly from the Parser class.

Parser.registerPreset()

Register a new preset rule. This allows STP to be extended globally. Presets don't handle the replacing, only the matching. STP comes with three pre-included presets: tag, url, and email.

Parser.registerPreset(name, match);
  • name (String) - The string id of the preset. Also the type.
  • match (String, RegExp, Function) - The search to perform.

Instance Methods

These methods can be called on objects returned from new Parser().

parser.addRule()

Add a parsing rule.

parser.addRule(match, replace);
  • match (String, RegExp, Function) - The search to perform. If a String, it is searched for exactly. If RegExp, a simple match is performed. If Function, it is called with a single argument: the full string passed to parse().
  • replace (String, Function) - Replaces the match when found. When a String, it is replaces exactly. Functions are called with matched substring as first argument. All capture groups are passed as next parameters.

parser.addPreset()

Registers a preset rule within the instance.

parser.addPreset(name, replace);
  • name (String) - The string id of the preset. Also the type.
  • replace (String, Function) - Replaces the match when found.

parser.toTree()

Returns the parsed string as an array of objects describing each part. Every part includes at least a type and text key. type defaults to text. The text key is used to replaced the matched string.

parser.toTree(str);
  • str (String) - A plain text string to parse.

parser.parse()

Returns a parsed string with all rules replaced.

parser.parse(str);
  • str (String) - A plain text string to parse.