Giter Site home page Giter Site logo

regexp's Introduction

Substance RegExp

This tiny library was created out of a frustration with the native API for regular expressions in Javascript.

Here's why:

For the new Substance.Surface we had to implement a simple lookup method that scans for word boundaries in a paragraph (in order to find out where to jump when pressing alt+right in our text editor).

So we came up with a simple regular expression.

var wordBounds = /\b\w/g;

In order to get all matches of a regular expression on a string you have to call RegExp.exec multiple times, which is quite error-prone and terrible to write.

var str = "Fix problem quickly with galvanized jets";
var matches = [];
// Execute until last match has been found
while ((match = wordBounds.exec(str)) !== null) {
  matches.push(match);
}

It's also quite unintuitive to access the information you need. In our case we needed to know the character positions of the matched word boundaries. Inspired by Phython and Ruby we came up with something that feels more natural, but without adding any magic.

var wordBounds = new Substance.RegExp(/.\w/g);
var matches = wordBounds.match(str);

Much better. Now if our cursor were at position 6 and we wanted to jump to next word bound we could find out the position by inspecting the normalized matches.

var cursorPos = 6;
var nextBound = _.find(matches, function(m) {
  return m.index>charPos;
}).index;

Easy. If you want to see it in action have a look at the brand-new Selection API.

regexp's People

Contributors

michael avatar oliver7654 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

imclab dabbott

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.