正则表达式测试
在线测试正则表达式:实时高亮匹配、计数,支持 g/i/m/s/u 标志,内置邮箱、网址、手机号等常用模板。
全程本地计算;正则语法以 JavaScript(ECMAScript)为准。
中文介绍
正则表达式(regex)是用一种模式去匹配、查找或校验文本的语言,处理日志、校验表单、清洗数据时几乎离不开。但正则写错了很容易「看起来能用、实则漏配或误配」。这个工具让你边写边看:输入模式和测试文本,匹配到的部分会实时高亮,并统计匹配了几处。
支持 g(全局)、i(忽略大小写)、m(多行)、s(点号匹配换行)、u(Unicode)五种标志,内置邮箱、网址、IPv4、中国手机号、日期、中文等常用模板。
常见问题
g 标志有什么用?
不加 g 只匹配第一处;加 g 会匹配全文所有位置,本工具的计数也依赖 g。比如 \d+ 对「a12b34」:不加 g 只匹配 12,加 g 匹配 12 和 34 两处。
\d、\w 分别代表什么?
\d 匹配一个数字(0-9),\w 匹配字母、数字或下划线,\s 匹配空白。大写 \D、\W、\S 表示「取反」。点号 . 匹配除换行外的任意字符(加 s 标志后也匹配换行)。
为什么我的正则报错?
常见原因:括号/方括号没闭合、量词位置不对(如 * 用在开头)、用了当前环境不支持的语法。报错信息会直接显示在下方,按提示改即可。
English
A regular expression (regex) is a pattern language for matching, finding or validating text — essential for parsing logs, validating forms and cleaning data. But a subtly wrong regex can "look fine" while silently missing or over-matching. This tool lets you test as you type: enter a pattern and test text, and matches are highlighted live with a count.
It supports the g (global), i (case-insensitive), m (multiline), s (dotall) and u (unicode) flags, plus ready-made patterns for email, URL, IPv4, Chinese phone numbers, dates and Chinese characters.
FAQ
What does the g flag do?
Without g only the first match is returned; with g every match is found. The counter here relies on g. For example \d+ on "a12b34": without g it matches only 12, with g it matches 12 and 34.
What do \d and \w mean?
\d matches a digit (0-9), \w matches a letter, digit or underscore, and \s matches whitespace. The uppercase \D, \W, \S are their negations. The dot . matches any character except newline (with the s flag it also matches newline).
Why does my regex throw an error?
Common causes: unclosed brackets, a quantifier in the wrong position (e.g. * at the start), or syntax not supported by the environment. The error message is shown below — fix accordingly.