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 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-regexp

v0.1.5

Published

A chainable regular expression wrapper for text search and replacements.

Downloads

4

Readme

Simple Regexp

Description

A chainable regular expression wrapper for text search and replacement.

Examples

var srxp = require('simple-regexp'); 
srxp('hi this is a simple text').word().include('s'); // ['this', 'is', 'simple']

or finds all links in a html document without rel="nofollow"

srxp(html).between('<a', '>').exclude('rel="nofollow"').between('href="', '"').matches();

or do a text replacement

srxp('where is my dog?').match(/dog/g).replace('cat').text(); //where is my cat?

Installation

npm:

npm install simple-regexp

bower:

bower install simple-regexp

Or just copy dist/srxp.js to you javascript folder.

API

General

The pattern in-parameter

Some metod has one or more pattern as in-parameter. Patterns are either a string or RegExp.

Strings are converted before turned into a regular expression. Single spaces are turn into one or more spaces, tabs, newlines (\s+). Special chars as [, ( are escaped. The global flag is used.

RegExp are used as they are with flags.

Chaining

srxp instances are chainable to make it simple to write complex searches in a easy and understandable way. See examples above.

srxp(text)

Returns a simple regexp instance. The inparameter is the text to use as source.

.between(startpattern, endpattern)

Find text between patterns. The start and end patterns are paired

srxp('a (b c (d) e)').between('(', ')').matches();     //['b c (d) e', 'd']

compared with a simple regexp where pairing of patterns is hard

'a (b c (d) e)'.match(/\((.*)\)/);      //["(b c (d) e)", "b c (d) e"]
'a (b c (d) e)'.match(/\((.*?)\)/);     //["(b c (d)", "b c (d"]

It might be possible with recursive regexp, ?R, but it's not supported in js.

.exclude(pattern)

Exclude matches with pattern

srxp('abcd aBcd abcd').between('a','d').exclude('B').matches();  //['bc', 'bc']

.include(pattern)

Include only matches including the pattern.

srxp('abcd aBcd abcd').between('a','d').include('B').matches();     //['Bc'])

It's similar to match but returns the whole string instead of only the matches.

srxp('abcd aBcd abcd').between('a','d').match('B').matches();     //['B'])

.match(pattern1, pattern2 ...)

Match patterns and adds them to matches. Several patterns can be used to match one pattern or another.

srxp('ab bb cb cc').match('ab', 'cb').matches();    //['ab', 'cb']

to match with and you have to chain the matches

srxp('ab bb cb cc').match(/.b/g).match(/c./g).matches()    //['cb']

.matches()

Returns the array of matches. See most examples.

.replace(newValue)

replaces the matched strings. To fetch the source with the replaced strings use .text()

srxp('abcdefghij').match('a', 'e', 'i').replace('VOWEL').text() //VOWELbcdVOWELfghVOWELj

newValue can also be a array and the replaces are user from the array.

srxp('abcdefghij').match('a', 'e', 'i').replace(['V1', 'V2']).text() //V1bcdV2fghij

Or a function.

var replace = function(text, index, length){
    return text.toUpperCase() + '@' + index;
};
srxp('abcdefghij').match(/[aei]/g).replace(replace).text() //A@0bcdE@4fghI@8j

.size(atLeast, atMost)

Include only matches with given length.

  var str = 'abcd abc abcd';
  srxp(str).word().size(3).matches();              // exact 3 chars ['abc']
  srxp(str).word().size(4,6).matches();            // 4 to 6 chars ['abcd', 'abcd']
  srxp(str).word().size(undefined, 3).matches();   // less or equal 3 chars ['abc'];
  

.text()

returns the source text including replacements, se .replace() above.

.word()

Splits a string into words. Alias for .match(/[\w]+/g).

srxp.trim(text)

Returns a string with spaces, tabs and newlines removed from start and end of the string. It also converts multiple spaces, tabs and newlines into one space.

Development

  • npm install to install dependencies
  • grunt run to build, start development server and watch for changes

To test the script in a browser run grunt run and then in your browser navigate to

data:text/html,<html><script type="text/javascript" src="http://localhost:8888/srxp.js"></script></html>

And do the testing in the developer console.

Test

There are some test coverage. It could of course be more comprehensive

grunt test