npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reverse-regexp-js

v1.0.5

Published

使用正则表达式生成随机数据

Downloads

2

Readme

Reverse Regexp JS

从正则表达式生成随机数据

Java版项目地址

https://github.com/GitHub-Laziji/reverse-regexp

安装

npm i reverse-regexp-js

使用

随机字符语法

支持大部分正则表达式的匹配语法

  • \d 数字, 相当于[0-9]
  • \w 数字、字母加下划线, 相当于[0-9a-zA-Z_]
  • \s 空白字符, 只包含空格和制表符
  • .\n\r以外的任意字符, 生成随机字符时只在ascii0~255之间生成
  • [a-zA-Z甲乙] 区间, 不支持^语法
  • 以及其他字符

重复打印语法

与正则表达式的重复匹配语法相同

  • ? 随机生成0个或1个字符
  • * 随机生成0个以上字符, 默认最多16个
  • + 随机生成1个以上字符, 默认最多16个
  • {n} 生成n个字符
  • {n,} 随机生成n~个字符, 默认最多max(16,n)
  • {n,m} 随机生成n~m个字符

其他语法

  • | 或语法, 例如aaa|bbb|ccc随机生成aaabbbccc, 概率相等
  • () 支持括号

常用正则

  • 邮箱 \w{6,12}@[a-z0-9]{3}\.(com|cn)
  • 手机号 1(3|5|7|8)\d{9}
  • 电话 \d{3}-\d{8}|\d{4}-\d{7}
  • 英文名 [A-Z][a-z]{4,6}
  • 年龄 [1-9][0-9]?
  • 网址 https?://[\w-]+(\.[\w-]+){1,2}(/[\w-]{3,6}){0,2}(\?[\w_]{4,6}=[\w_]{4,6}(&[\w_]{4,6}=[\w_]{4,6}){0,2})?
  • IPv4 (\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}
import Node from "reverse-regexp-js";

function random(expression, title) {
  console.log(`${title} ${expression}`);
  let node = Node.compile(expression);
  for (let i = 0; i < 10; i++) {
    let data = node.random();
    console.log(`[${new RegExp(`^(${expression})$`).test(data)}] ${data}`);
  }
  console.log();
}

random("\\w{6,12}@[a-z0-9]{3}\\.(com|cn)", "邮箱");
random("1(3|5|7|8)\\d{9}", "手机号");
random("-?[1-9]\\d*\\.\\d+", "浮点数");
random("https?://[\\w-]+(\\.[\\w-]+){1,2}(/[\\w-]{3,6}){0,2}(\\?[\\w_]{4,6}=[\\w_]{4,6}(&[\\w_]{4,6}=[\\w_]{4,6}){0,2})?", "网址");
邮箱 \w{6,12}@[a-z0-9]{3}\.(com|cn)
[true] [email protected]
[true] [email protected]
[true] [email protected]
[true] [email protected]
[true] [email protected]
[true] [email protected]
[true] [email protected]
[true] [email protected]
[true] [email protected]
[true] [email protected]

手机号 1(3|5|7|8)\d{9}
[true] 18394387666
[true] 17198794815
[true] 13302320996
[true] 13993463722
[true] 13659131576
[true] 13210648735
[true] 17757142513
[true] 13120357027
[true] 15463373047
[true] 13229626283

浮点数 -?[1-9]\d*\.\d+
[true] 9544253739281.825854324
[true] -3514630221392761.41656638159843
[true] 87202.860
[true] 446.4
[true] -62172304824.681
[true] 1793916390807638.2060694602070
[true] 10464.5830884
[true] -287164572.642271169910
[true] 102580279.641238930471986
[true] -657934186.62926606731880

网址 https?://[\w-]+(\.[\w-]+){1,2}(/[\w-]{3,6}){0,2}(\?[\w_]{4,6}=[\w_]{4,6}(&[\w_]{4,6}=[\w_]{4,6}){0,2})?
[true] https://JpgCnSnnw8eaPe.vzCZaTOXfVQ_mYm.wRGZ3R0Bxs5skoFh/nV1
[true] https://kUUWuge4-.lzrXcs/L1bZRu?o08xxa=YDH6&sCKy1W=35ltOM
[true] http://xVbVmsnrX1f.cfCS.RE6yZWBW/bnX0u
[true] http://xGuQDvYfONr.Vr.i2?6ybZ=DGhM84&ckaC=6pXv
[true] http://lQUoMep2rrgps.gBI_wm.zcvdLkV/1OUmj9?hSqy=Qhlv&who8b=c4NzD&WyxSHH=0BgY
[true] http://th6MAgZIsF.rZIBz1.aRoDj0P-T?xrkqjx=h0DfY&SOArT=P7cMxf
[true] http://IXx1g40PRnz1P.r-1.a/iFR9L/J3k?TyNMi=EvSf47&y1B_=5l7w8_
[true] http://8X7Gxr7.nttaveUk9LANflC_/coAY
[true] https://uf.w6jhBFn-dj3o/DxAa3/0LBbrO
[true] http://2xBXYA.jNT.9TMzjz?v_k5J=1mRRO&h7OA=4YSr