
Product
Introducing Scala and Kotlin Support in Socket
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
Generate a string match the given regular expression, and also parse the regexp to AST segments.
Parse a regular expression to rule parts, and build a string match it. You can use it to mock a string with the given regular expression. it strictly abide by the real RegExp rules of the javascript language.Please see theSpecial cases
reregexp 主要用来解析正则表达式,根据正则表达式生成一个相匹配的随机字符串。
npm install --save reregexp
const RegexpParser = require('reregexp').default;
const r1 = new RegexpParser(/([a-z0-9]{3})_\1/);
r1.build();
// will ouput random string like this: 'a2z_a2z', '13d_13d'
const r2 = new RegexpParser(/(?<named>\w{1,2})_\1_\k<named>/);
r2.build();
// will output random string like this: 'b5_b5_b5', '9_9_9'
const r3 = new RegexpParser('/(a)\\1(?<named>b)\\k<named>(?<override>\\w+)/', {
namedGroupConf: {
override: ['cc', 'dd'],
},
});
r3.build();
// will output random string "aabbcc" or "aabbdd"
const r4 = new RegexpParser(/[^\w\W]+/);
r4.build();
// will throw error, because the [^\w\W] will match nothing.
const r5 = new RegexpParser(/[^a-zA-Z0-9_\W]/);
r5.build();
// will throw error, this is the same as [^\w\W]
const r6 = new RegexpParser(/[a-z]{3}/i);
r6.build();
// will output random string like this: 'bZD' 'Poe'
RegexpParser.maxRepeat = 10;
const r7 = new RegexpParser(/a*/); // 'a' will repeated at most 10 times.
// will output: 'aaaaa'
const r8 = new RegexpParser(/a*/, {
maxRepeat: 20, // 'a' will repeated at most 20 times
});
// extractSetAverage
const r9 = new RegexpParser(/[\Wa-z]/, {
extractSetAverage: true, // \W will extract as all the characters match \W, a-z now doesn't have the same chance as \W
});
i
ignore case, /[a-z]/i
is same as /[a-zA-Z]/
u
unicode flag
s
dot all flag
the flags g
m
y
will ignore.
.build()
build a string that match the regexp.
.info()
get a regexp parsed queues, flags, lastRule after remove named captures.
{
rule: '',
context: '',
flags: [],
lastRule: '',
queues: [],
}
^
$
the start,end anchors will be ignored.(?=)
(?!)
(?<=)
(?<!)
the regexp lookhead,lookbehind will throw an error when run build()
.\b
\B
will be ignored./\1(o)/
the capture group \1
will match null, the build()
will just output o
, and /^\1(o)$/.test('o') === true
/(o)\1\2/
the capture group \2
will treated as code point of unicode. so the build()
will output oo\u0002
. /^(o)\1\2$/.test('oo\u0002') === true
/(o\1)/
the capture group \1
will match null, build()
will output o
, /^(o\1)$/.test('o') === true
/[]/
empty character class, the build()
method will throw an error, because no character will match it.
/[^]/
negative empty character class, the build()
method will output any character.
/[^\w\W]/
for the negative charsets, if all the characters are eliminated, the build()
will throw an error. the same such as /[^a-zA-Z0-9_\W]/
、/[^\s\S]/
...
Welcome to report to us with issue if you meet any question or bug. Issue
FAQs
Generate a random string match a given regular expression, suitable for mocking strings.
The npm package reregexp receives a total of 2,432 weekly downloads. As such, reregexp popularity was classified as popular.
We found that reregexp 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.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
Application Security
/Security News
Socket CEO Feross Aboukhadijeh and a16z partner Joel de la Garza discuss vibe coding, AI-driven software development, and how the rise of LLMs, despite their risks, still points toward a more secure and innovative future.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.