Giter Club home page Giter Club logo

node-pcre's Introduction

Description

A pcre binding for node.js with UTF8 and Unicode properties support.

Requirements

  • node.js -- v0.8.0 or newer
  • Windows, Linux, or OSX
    • BSD or OpenSolaris support is possible -- just need to generate and submit a config.h for PCRE 8.32 with these options:
./configure --enable-utf8 --enable-unicode-properties --enable-static --disable-shared --enable-jit --disable-cpp --enable-pcre8 --disable-pcre16 --disable-pcre32

Install

npm install pcre

Examples

  • Simple one-off regexp execution:
var inspect = require('util').inspect;
var PCRE = require('pcre').PCRE;

console.log(inspect(PCRE.exec("(?<nodejsrules>o)", "foo", 0), false, Infinity));

// output:
// [ 1, 2, 1, 2, named: { nodejsrules: [ 1, 2 ] } ]
  • Simple one-off regexp execution returning all matches:
var inspect = require('util').inspect;
var PCRE = require('pcre').PCRE;

console.log(inspect(PCRE.execAll("(?<nodejsrules>o)", "foo", 0), false, Infinity));

// output:
// [ [ 1, 2, 1, 2, named: { nodejsrules: [ 1, 2 ] } ],
//   [ 2, 3, 2, 3, named: { nodejsrules: [ 2, 3 ] } ] ]
  • Instantiate a regexp and test it:
var PCRE = require('pcre').PCRE;

var re = new PCRE("o");
console.log(re.test("foo", 0));
console.log(re.test("bar", 0));
console.log(re.test("node.js rules", 2));

// output:
// true
// false
// false
  • Instantiate a regexp, JIT compile it, and execute it, returning all matches:
var inspect = require('util').inspect;
var PCRE = require('pcre').PCRE;

var re = new PCRE("o");
re.study(PCRE.PCRE_STUDY_JIT_COMPILE);
console.log(inspect(re.execAll("fooooo", 0), false, Infinity));

// output:
// [ [ 1, 2 ], [ 2, 3 ], [ 3, 4 ], [ 4, 5 ], [ 5, 6 ] ]

API

PCRE static constants

All static constants for regexp flags/options and errors can be found in lib/pcre.js.

PCRE static methods

  • exec(< string >pattern, < mixed >subject, < integer >offset[, < integer >flags]) - mixed - Compiles pattern and executes it on subject starting at offset in subject. subject can be a string or Buffer. The return value is either null in case of no match, an integer error code in case of error, or an array on success containing offsets in the subject for the first match. The first two offsets reference the entirety of the matched part of the subject. Any additional offsets reference capture groups in order from left to right. Offsets for named capture groups are additionally available on the named object.

  • execAll(< string >pattern, < mixed >subject, < integer >offset[, < integer >flags]) - mixed - Same as exec() except an array of array matches is returned on success.

  • test(< string >pattern, < mixed >subject, < integer >offset[, < integer >flags]) - boolean - Similar to exec(), but used merely to test if pattern matches at least once.

  • version() - string - Returns the version and date of the PCRE library used (e.g. "8.32 2012-11-30").

PCRE methods

  • (constructor)(< string >pattern[, < integer >flags]) - Compiles pattern and returns a new PCRE instance.

  • study([< integer >flags][, < integer >jitStackStart=1, < integer >jitStackMax=32KB]) - boolean - Performs some analysis of the compiled regexp in order to optimize it. jitStackStart and jitStackMax are custom starting and maximum JIT stack sizes (in bytes) respectively for when one of the JIT flags are passed in. The return value indicates the success of the analysis.

  • set(< string >pattern[, < integer >flags]) - (void) - Compiles a new pattern and replaces the existing regexp.

  • save() - Buffer - Returns the internal state object representing the compiled regexp. Note: this does not save the result of any previous optimizations performed by study().

  • load(< Buffer >state) - (void) - Loads previously saved internal state data from save().

  • exec(< mixed >subject, < integer >offset[, < integer >flags]) - mixed - Similar to PCRE.exec().

  • execAll(< mixed >subject, < integer >offset[, < integer >flags]) - mixed - Same as exec() except an array of array matches is returned on success.

  • test(< mixed >subject, < integer >offset[, < integer >flags]) - boolean - Similar to exec(), but used merely to test if pattern matches at least once.

node-pcre's People

Contributors

mscdex avatar

Watchers

 avatar

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.