Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
The ret npm package is a library for tokenizing regular expressions. This means it can parse regular expressions into a structured format, making it easier to analyze, manipulate, or transform them programmatically. It's particularly useful for developers working with dynamic or complex regular expressions, offering a way to understand and manipulate the patterns in a more granular and controlled manner.
Tokenization of Regular Expressions
This feature allows you to tokenize a regular expression, breaking it down into its constituent parts. The code sample demonstrates how to tokenize a simple regular expression that matches 'hello' or 'world', case-insensitively. The result is a structured representation of the regex, including its type, sub-expressions, and flags.
const ret = require('ret');
const tokens = ret(/hello|world/i);
console.log(tokens);
Analysis of Character Classes
With ret, you can also analyze character classes within regular expressions. The code sample shows how to tokenize a regex that matches any lowercase letter from 'a' to 'z'. The output will detail the structure of the character class, including its range and any specified characters.
const ret = require('ret');
const tokens = ret(/[a-z]/);
console.log(tokens);
Handling of Quantifiers
This functionality allows for the parsing and understanding of quantifiers within regular expressions. The provided code sample tokenizes a regex that matches between two and four digits. The tokenized output includes detailed information about the quantifier, such as its type and the minimum and maximum number of repetitions.
const ret = require('ret');
const tokens = ret(/\d{2,4}/);
console.log(tokens);
regexpp is a regular expression parser with a focus on providing a detailed abstract syntax tree (AST) for regular expressions. It offers similar functionality to ret in terms of parsing regular expressions but goes further by providing a more comprehensive AST, making it suitable for more complex analysis and manipulation tasks.
regexp-tree is a toolkit for working with regular expressions in JavaScript. It includes a parser, a regexp transformer, and a regexp compatibility transpiler. Compared to ret, regexp-tree offers a broader range of features, including the ability to transform and optimize regular expressions, making it a more versatile tool for developers.
Tokenizes strings that represent a regular expressions.
var ret = require('ret');
var tokens = ret(/foo|bar/.source);
tokens
will contain the following object
{
"type": ret.types.ROOT
"options": [
[ { "type": ret.types.CHAR, "value", 102 }
, { "type": ret.types.CHAR, "value", 111 }
, { "type": ret.types.CHAR, "value", 111 } ],
[ { "type": ret.types.CHAR, "value", 98 }
, { "type": ret.types.CHAR, "value", 97 }
, { "type": ret.types.CHAR, "value", 114 } ]
]
}
ret.types
is a collection of the various token types exported by ret.
Only used in the root of the regexp. This is needed due to the posibility of the root containing a pipe |
character. In that case, the token will have an options
key that will be an array of arrays of tokens. If not, it will contain a stack
key that is an array of tokens.
[optional]
{
"type": ret.types.ROOT
, "stack": [token]
}
Groups contain tokens that are inside of a parenthesis. If the group begins with ?
followed by another character, it's a special type of group. A ':' tells the group not to be remembered when exec
is used. '=' means the previous token matches only if followed by this group, and '!' means the previous token matches only if NOT followed.
Like root, it can contain an options
key instead of stack
if there is a pipe.
{
"type": ret.types.GROUP
, "remember" true
, "followedBy": false
, "notFollowedBy": false
, "options" [[token]]
}
\b
, \B
, ^
, and $
specify positions in the regexp.
{
"type": ret.types.POSITION
, "value": "^"
}
Contains a key set
specifying what tokens are allowed and a key not
specifying if the set should be negated.
{
"type": ret.types.SET
, "set": [token]
, "not": false
}
Used in set tokens to specify a character range. from
and to
are character codes.
{
"type": ret.types.RANGE
, "from": 97
, "to": 122
}
{
"type": ret.types.REPETITION
, "min": 0
, "max": Infinity
}
References a group token. value
is 1-9.
{
"type": ret.types.REFERENCE
, "value": 1
}
Represents a single character token. value
is the character code. This might seem a bit cluttering instead of concatenating characters together. But since repetition tokens only repeat the last token and not the last clause like the pipe, it's simpler to do it this way.
{
"type": ret.types.CHAR
, "value": 123
}
npm install ret
Tests are written with vows
npm test
MIT
FAQs
Tokenizes a string that represents a regular expression.
The npm package ret receives a total of 12,414,282 weekly downloads. As such, ret popularity was classified as popular.
We found that ret demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.