0.0.7 • Published 9 months ago

semantic-regex v0.0.7

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

Semantic Regex

A semantic regex compiler for javascript

Install

npm i semantic-regex

Basic Syntax

[=semantic_key=]

How to Use

You may use the semantic regex to match a specific pattern, such as:

import SementicRegex from 'semantic-regex';

// if you're using node.js
// const SementicRegex = require('semantic-regex');

console.log(/[=ipv4=]/.test('127.0.0.1'));               // true
console.log(/[=ipv4=]/.test('127.0.999.1'));             // false
console.log('hello 127.0.0.1 1992'.match(/[=ipv4=]/g));   // ['127.0.0.1']

In this example, [=ipv4=] is equivolent to (?:(?:(?:2(?:5[0-5]|[1-4]\d)|1?[1-9]?\d))(?:\.(?:(?:2(?:5[0-5]|[1-4]\d)|1?[1-9]?\d))){3}) (ipv4 regex)

You may also register your own aliases for your regex (make sure to wrap them in non-capturing group (?:) to make your pattern quantifiable):

import { register } from 'semantic-regex';

register({
	'year': /(?:[12]\d\d\d)/
});

console.log(/[=year=]/.test('1995'));                      // true
console.log(/[=year=]/.test('3995'));                      // false
console.log('hello 127.0.0.1 1992'.match(/[=year=]/g));    // ['1992']

// you can use those aliases combined with normal regex
console.log(/[=year=]\s[=ipv4=]/.test('1992 127.0.0.1'));  // true

Built-in Aliases

aliasnoteexample
ipv4Match IPv4127.0.0.1, 192.168.1.100, 36.79.100.9
emailMatch a common email addresstest@example.com, alice@test.co.jp
zhMatch a chinese character,
urlMatch a url pathhttps://example.com/test?foo=bar&a=b#123, youtube.com

To-do

  • Add more built-in commonly-used patterns
  • Types?
  • Add more test cases
0.0.7

9 months ago

0.0.6

9 months ago

0.0.5

9 months ago

0.0.4

9 months ago

0.0.3

9 months ago

0.0.2

9 months ago

0.0.1

9 months ago