Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/taufik-nurrohman/pattern
Regular expression utility.
https://github.com/taufik-nurrohman/pattern
expression helper node pattern regex regexp regular utility
Last synced: 17 days ago
JSON representation
Regular expression utility.
- Host: GitHub
- URL: https://github.com/taufik-nurrohman/pattern
- Owner: taufik-nurrohman
- License: mit
- Created: 2020-11-21T00:19:35.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-01-04T13:45:17.000Z (10 months ago)
- Last Synced: 2024-09-25T16:09:33.710Z (about 1 month ago)
- Topics: expression, helper, node, pattern, regex, regexp, regular, utility
- Language: JavaScript
- Homepage:
- Size: 37.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Regular Expression Utility
==========================Usage
-----### CommonJS
~~~ js
const {toPattern} = require('@taufik-nurrohman/pattern');console.log(toPattern('[a-z]+'));
~~~### ECMAScript
~~~ js
import {toPattern} from '@taufik-nurrohman/pattern';console.log(toPattern('[a-z]+'));
~~~Methods
-------### esc(pattern, extra = "")
Escape regular expression characters in string except `\`.
### escChar(pattern, extra = "")
Escape regular expression characters for character class only. E.g. `^`, `[`, `]`, and `-`.
### extractInner(content, start, end, opt = "")
Extract nesting constructs in a string.
~~~ js
console.log(extractInner('a(b(c))d(e)f((g)h)', '\\(', '\\)'));
~~~~~~ js
[
['b(c)', [
['c', [], 2, 3]
], 2, 6],
['e', [], 9, 10],
['(g)h', [
['g', [], 1, 2]
], 13, 17]
]
~~~### extractTokens(content, tokens, defaultTokenName = 0)
~~~ js
console.log(extractTokens('This is an active link and an empty link.', [
['(<)([\\w:-]+)(\\s[^>]*?)?(>)', ['tag', 'p', 'name', [
['"[^"]+"', 'value'],
['=', 'p'],
['[\\w:-]+', 'key']
], 'p']],
['(<)(/)([\\w:-]+)(>)', ['tag', 'p', 'p', 'name', 'p']]
]));
~~~~~~ js
[
[['This is an '], [0]],
[['', '<', 'a', ' href="https://example.com"', '>'], ['tag', 'p', 'name', [
[[' '], [0]],
[['href'], ['key']],
[['='], ["p"]],
[['"https://example.com"'], ['value']]
], 'p']],
[['active link'], [0]],
[['', '<', '/', 'a', '>'], ['tag', 'p', 'p', 'name', 'p']],
[[' and an '], [0]],
[['', '<', 'a', undefined, '>'], ['tag', 'p', 'name', 0, 'p']],
[['empty link'], [0]],
[['', '<', '/', 'a', '>'], ['tag', 'p', 'p', 'name', 'p']],
[['.'], [0]]
]
~~~### fromPattern(pattern)
Get pattern from `RegExp` as string.
### isPattern(x)
Detect if `x` is an instance of `RegExp`.
### token(start, content|contents, end = start, skip)
Build pattern string from parameters.
~~~ js
console.log(token('"'));
~~~### tokenGroup(start, content|contents, end = start, skip)
Build pattern string from parameters as a group of `start`, `content` and `end`.
~~~ js
console.log(tokenGroup('"'));
~~~### toPattern(string, opt = 'g')
Convert pattern string to `RegExp` instance.
### x
List of regular expression’s special characters except `\`.