
# Mock语法说明
APIMock语法完全兼容Mock.js，并扩展了一些Mock.js没有的语法（uuid、正则表达式），Mock语法详见[表1]。
如现有Mock语法无法满足需求，建议使用正则表达式"@regexp"来实现灵活定制，正则表达式基本能满足各种特殊场景的需求。
 表1Mock语法 
| 类型      | 函数                                                                                     |
|:---|:---|
| Basic   | boolean、natural、integer、float、character、string、range、date、time、datetime、now、timestamp。 |
| Name    | first、last、name、cfirst、clast、cname。                                                    |
| Web     | url、domain、email、ip、tld。                                                               |
| Address | region、province、city、county。                                                           |
| Text    | paragraph、sentence、word、title、cparagraph、csentence、cword、ctitle。                       |
| Color   | color。                                                                                 |
| Other   | guid, id,uuid。                                                                         |
| Helper  | capitalize、upper、lower、pick、shuffle。                                                   |
| Reg     | regex。                                                                                 |
   
#### 约束与限制
使用正则表达式时，"regexp"特殊字符需要加上"\\"进行转义。
#### 基本写法
- 支持json。函数需要写在value值位置，以"@"作为起始符。
 
- 支持xml。（本版本不支持，下个版本支持。）
- 不支持单独@函数。
- 只支持在响应体内使用mock.js。
实例如下：
| 输入                                                                                                          | 示例结果                                                                                                              |
|:---|:---|
| ``` { "id":@uuid(), "flag":@boolean(1, 9, true), "name":@string("teststart",3,7), "createtime":@now() } ``` | ``` { "id":d29925a288404207b60161fdaed05d55 "flag":false, "name":"stseee", "createtime":2022-08-11 15:16:20 } ``` |
   
#### 基础类型
| 分类  | 规则                        | 示例                    | 示例结果                    |
|:---|:---|:---|:---|
| 布尔值 | @boolean                  | @boolean()            | false，true              |
| 布尔值 | @boolean(min,max,current) | @boolean(1,9,true)    | false，true              |
| 自然数 | @natural                  | @natural()            | 1350447356              |
| 自然数 | @natural(min)             | @natural(10000)       | 819989031               |
| 自然数 | @natural(min,max)         | @natural(60,100)      | 63                      |
| 整数  | @integer                  | @integer              | 1128800169              |
| 整数  | @integer(min)             | @integer(10000)       | 29427959                |
| 整数  | @integer(min,max)         | @integer(60,100)      | 94                      |
| 浮点数 | @float                    | @float()              | 13425418.3              |
| 浮点数 | @float(min)               | @float(0)             | 1800545158.8            |
| 浮点数 | @float(min,max)           | @float(60,100)        | 98.63203                |
| 浮点数 | @float(min,max,dmin)      | @float(60,100,3)      | 69.882191               |
| 浮点数 | @float(min,max,dmin,dmax) | @float(60,100,3,5)    | 80.14955                |
| 单字符 | @character                | @character()          | "7"                     |
| 单字符 | @character(pool)          | @character("lower")   | "x"                     |
| 单字符 | @character(pool)          | @character("upper")   | "R"                     |
| 单字符 | @character(pool)          | @character("number")  | "6"                     |
| 单字符 | @character(pool)          | @character("symbol")  | "#"                     |
| 单字符 | @character(pool)          | @character("aeiou")   | "i"                     |
| 字符串 | @string                   | @string()             | "%#Vwj"                 |
| 字符串 | @string(length)           | @string(5)            | "\^16)1"                |
| 字符串 | @string(pool,length)      | @string("lower",5)    | "zrcmq"                 |
| 字符串 | @string(pool,length)      | @string("upper",5)    | "PFTFP"                 |
| 字符串 | @string(pool,length)      | @string("number",5)   | "96480"                 |
| 字符串 | @string(pool,length)      | @string("symbol",5)   | "#\&!!("                |
| 字符串 | @string(pool,length)      | @string("aeiou",5)    | "uoauu"                 |
| 字符串 | @string(min,max)          | @string(7,10)         | "0B(6!)5\[)"            |
| 字符串 | @string(pool,min,max)     | @string("lower",1,3)  | "zz"                    |
| 字符串 | @string(pool,min,max)     | @string("upper",1,3)  | "OZJ"                   |
| 字符串 | @string(pool,min,max)     | @string("number",1,3) | "61"                    |
| 字符串 | @string(pool,min,max)     | @string("symbol",1,3) | "@%"                    |
| 字符串 | @string(pool,min,max)     | @string("aeiou",1,3)  | "au"                    |
| 范围  | @range(stop)              | @range(10)            | \[0,1,2,3,4,5,6,7,8,9\] |
| 范围  | @range(start,stop)        | @range(3,7)           | \[3,4,5,6\]             |
| 范围  | @range(start,stop,step)   | @range(1,10,3)        | \[1,4,7\]               |
   
#### 日期/时间
| 分类   | 规则                 | 示例                                    | 示例结果                      |
|:---|:---|:---|:---|
| 日期   | @date              | @date()                               | "2015-05-09"              |
| 日期   | @date(format)      | @date("yyyy-MM-dd")                   | "2012-11-08"              |
| 日期   | @date(format)      | @date("yy-MM-dd")                     | "10-06-12"                |
| 日期   | @date(format)      | @date("yyyy yy y MM M dd d")          | "1971 71 71 05 5 02 2"    |
| 时间   | @datetime          | @datetime()                           | "1996-15-11 01:18:47"     |
| 时间   | @datetime(format)  | @datetime("yyyy-MM-dd A HH:mm:ss")    | "1978-01-10 AM 03:59:54"  |
| 时间   | @datetime(format)  | @datetime("yy-MM-dd a HH:mm:ss")      | "98-09-03 下午 19:32:44"    |
| 当前时间 | @now               | @now()                                | "2022-11-30 12:46:12"     |
| 当前时间 | @now(unit)         | @now("year")                          | "2022-01-01 00:00:00"     |
| 当前时间 | @now(unit)         | @now("month")                         | "2022-08-01 00:00:00"     |
| 当前时间 | @now(unit)         | @now("week")                          | "2022-08-09 00:00:00"     |
| 当前时间 | @now(unit)         | @now("day")                           | "2022-08-11 00:00:00"     |
| 当前时间 | @now(unit)         | @now("hour")                          | "2022-08-11 15:00:00"     |
| 当前时间 | @now(unit)         | @now("minute")                        | "2022-08-11 15:24:00"     |
| 当前时间 | @now(unit)         | @now("second")                        | "2022-08-11 15:24:02"     |
| 当前时间 | @now(format)       | @now("yyyy-MM-dd HH:mm:ss SS")        | "2022-08-11 15:24:02 761" |
| 当前时间 | @now(unit,format)  | @now("day", "yyyy-MM-dd HH:mm:ss SS") | "2022-08-11 00:00:00 000" |
| 时间戳  | @timestamp(format) | @timestamp("s")                       | "1662605353"              |
| 时间戳  | @timestamp(format) | @timestamp("ms")                      | "1662605408838"           |
   
#### Web 相关
| 规则                  | 示例                      | 示例结果                           |
|:---|:---|:---|
| @url                | @url()                  | "http://ihum.md/xmicdyn"       |
| @url(protocol)      | @url("http")            | "http://ckpvjgyc.eg/jzatazzvm" |
| @url(protocol,host) | @url("http","test.com") | "http://test.com/pmuway"       |
| @protocol           | @protocol()             | "ftp"                          |
| @domain             | @domain()               | "ljmhm.gu"                     |
| @domain(tld)        | @domain("com")          | "dtcboprfg.com"                |
| @tld                | @tld()                  | "com"                          |
| @email              | @email()                | "e.fsysqt@vjz.edu"             |
| @email              | @email("test.com")      | "e.fsysqt@test.com"            |
| @ip                 | @ip()                   | "xxx.xxx.xxx.xxx"              |
   
#### 其他
| 分类    | 规则    | 示例    | 示例结果                                   |
|:---|:---|:---|:---|
| GUID  | @guid | @guid | "7C50585F-8DF1-4E94-B016-EE13BD36248D" |
| 身份证ID | @id   | @id   | "712424201708225548"                   |
| uuid  | @uuid | @uuid | "d29925a288404207b60161fdaed05d55"     |
   
#### Helper
| 分类  | 规则          | 示例                                  | 示例结果    |
|:---|:---|:---|:---|
| 全大写 | @upper(str) | @upper("hello")                     | "HELLO" |
| 全小写 | @lower(str) | @lower("HELLO")                     | "hello" |
| 多选一 | @pick(arr)  | @pick(\["hello", "test", "test3"\]) | "hello" |
| 多选一 | @pick(arr)  | @pick(\[1, 5, 10, 60, 80\])         | 10      |
| 多选一 | @pick(arr)  | @pick(\[1,"hello","中国",true,80\])   | true    |
   
#### Name相关
| 分类                                                 | 规则                                                    | 示例           | 示例结果                |
|:---|:---|:---|:---|
| 英文名                                                | @first                                                | @first()     | "Michelle"          |
| 英文姓                                                | @last                                                 | @last()      | "Williams"          |
| 英文姓名   | @name                                                 | @name()      | "Michelle Williams" |
| 英文姓名   | @name(middle)  | @name(true)  | "Karen Lisa Harris" |
| 英文姓名   | @name(middle)  | @name(false) | "Karen Harris"      |
| 中文姓                                                | @cfirst                                               | @cfirst()    | "张"                 |
| 中文名                                                | @clast()                                              | @clast()     | "三"                 |
| 中文姓名                                               | @cname                                                | @cname()     | "张三"                |
   
#### Address相关
| 分类                                               | 规则                                                       | 示例             | 示例结果          |
|:---|:---|:---|:---|
| 区域                                               | @region                                                  | @region()      | "西北"          |
| 省或直辖市、自治区、特别行政区                                  | @province                                                | @province()    | "陕西省"         |
| 市    | @city                                                    | @city()        | "西安市"         |
| 市    | @city(prefix)    | @city(true)    | "陕西省 西安市"     |
| 市    | @city(prefix)    | @city(false)   | "西安市"         |
| 县   | @county                                                  | @county()      | "蓝田县"         |
| 县   | @county(prefix)  | @county(true)  | "陕西省 西安市 蓝田县" |
| 县   | @county(prefix)  | @county(false) | "蓝田县"         |
| 邮政编码                                             | @zip                                                     | @zip()         | "710500"      |
   
#### 正则表达式
| 规则                | 示例                                    | 示例结果              |
|:---|:---|:---|
| @regexp( regexp ) | @regexp("\[0-9\]+")                   | "36436"           |
| @regexp( regexp ) | @regexp("\[0-9\]{3,5}")               | "343"             |
| @regexp( regexp ) | @regexp("\[a-z\]+\\\\@gmail\\\\.com") | "ifadt@gmail.com" |
   
| 支持类型    | 描述                                                                                |
|:---|:---|
| \*      | 匹配前一个表达式0次或多次，等价于{0,}。                                                            |
| .       | 默认匹配除换行符之外的任何单个字符（用来生成乱码）。                                                        |
| +       | 匹配前面一个表达式1次或者多次，等价于{1,}。                                                          |
| ?       | 匹配前面一个表达式0次或者1次，等价于{0,1}。                                                         |
| x\|y    | 匹配'x'或者'y'。                                                                       |
| {n}     | n是一个正整数，匹配前面一个字符刚好出现了n次。                                                          |
| {n,}    | n是一个正整数，匹配前面一个字符至少出现了n次。                                                          |
| {n,m}   | n和m都是整数，匹配前面的字符至少n次，最多m次。如果n或者m的值是0，该值被忽略。                                        |
| \[xyz\] | 一个字符集合，匹配方括号中的任意字符。                                                               |
| @       | 与"."的功能类似，默认匹配除换行符之外的任何单个字符（生成乱码）。                                                |
| \\      | 将下一个字符标记为特殊字符、原义字符、向后引用或八进制转义符。例如： 'n'匹配字符'n'，'\\n'匹配换行符，序列'\\'匹配""，而'\\('则匹配"("。 |
| \[      | 标记一个中括号表达式的开始。要匹配\[，请使用\[。                                                        |
| {       | 标记限定符表达式的开始。要匹配{，请使用{。                                                            |
   
| 不支持类型                | 描述                                                 |
|:---|:---|
| \^                   | 匹配输入的开始。                                           |
| $                    | 匹配输入的结束。                                           |
| (x)                  | 匹配'x'并且记住匹配项，其中括号被称为捕获括号。                          |
| (?:x)                | 匹配'x'但是不记住匹配项，其中括号被称为非捕获括号，能够定义与正则表达式运算符一起使用的子表达式。 |
| x(?=y)               | 匹配'x'仅仅当'x'后面跟着'y'，这种叫做先行断言。                       |
| (?\<=y)x             | 匹配'x'仅当'x'前面是'y'，这种叫做后行断言。                         |
| x(?!y)               | 仅仅当'x'后面不跟着'y'时匹配'x'，这被称为正向否定查找。                   |
| (?\<!y)x             | 仅仅当'x'前面不是'y'时匹配'x'，这被称为反向否定查找。                    |
| \[\^xyz\]            | 一个反向字符集，即匹配任何没有包含在方括号中的字符。                         |
| \[\\b\]              | 匹配一个退格(U+0008)。                                    |
| \\b                  | 匹配一个词的边界。                                          |
| \\B                  | 匹配一个非单词边界。                                         |
| \\cX                 | 当X处于A到Z之间的字符时，匹配字符串中的一个控制符。                        |
| \\d                  | 匹配一个数字，等价于\[0-9\]。                                 |
| \\D                  | 匹配一个非数字字符，等价于0-9。                                  |
| \\f                  | 匹配一个换页符(U+000C)。                                   |
| \\n                  | 匹配一个换行符(U+000A)。                                   |
| \\r                  | 匹配一个回车符(U+000D)。                                   |
| \\s                  | 匹配一个空白字符，包括空格、制表符、换页符和换行符。                         |
| \\S                  | 匹配一个非空白字符。                                         |
| \\t                  | 匹配一个水平制表符(U+0009)。                                 |
| \\v                  | 匹配一个垂直制表符(U+000B)。                                 |
| \\w                  | 匹配一个单字字符（字母、数字或者下划线），等价于\[A-Za-z0-9_\]。            |
| \\W                  | 匹配一个非单字字符，等价于\[A-Za-z0-9_\]。                       |
| \\n                  | 在正则表达式中，返回最后的第n个子捕获匹配的子字符串（捕获的数目以左括号计数）。           |
| \\0                  | 匹配NULL（U+0000）字符。不能在后面跟其它小数，因为\\0是一个八进制转义序列。       |
| \\xhh                | 匹配一个两位十六进制数（\\x00-\\xFF）表示的字符。                     |
| \\uhhhh              | 匹配一个四位十六进制数表示的UTF-16代码单元。                          |
| \\u{hhhh}或\\u{hhhhh} | 仅当设置了u标志时，匹配一个十六进制数表示的Unicode字符。                   |
   
![](https://support.huaweicloud.com/usermanual-apiarts/public_sys-resources/note_3.0-zh-cn.png)
- 参考文档：[正则表达式](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions)。
 
