mm_matchs 1.1.6 → 1.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +8 -3
- package/package.json +1 -1
- package/test.js +203 -167
package/index.js
CHANGED
|
@@ -298,7 +298,9 @@ Matchs.prototype.get = function(str, format) {
|
|
|
298
298
|
}
|
|
299
299
|
});
|
|
300
300
|
|
|
301
|
-
format = format.replace(/\r/g, '\\r').replace(/\n/g, '\\n').replace(/\*/g, '.*');
|
|
301
|
+
format = format.replace(/\r/g, '\\r').replace(/\n/g, '\\n').replace(/\*/g, '.*').replace(/\//g, '\\/');
|
|
302
|
+
|
|
303
|
+
// console.log("这啥1?", format);
|
|
302
304
|
// var fmt = format.replace(/\{[a-zA-Z\u4e00-\u9fa5_]+\}/g, '[\\s\\S]*');
|
|
303
305
|
try {
|
|
304
306
|
rxs.map((o) => {
|
|
@@ -310,6 +312,7 @@ Matchs.prototype.get = function(str, format) {
|
|
|
310
312
|
//TODO handle the exception
|
|
311
313
|
}
|
|
312
314
|
|
|
315
|
+
// console.log("这啥2?", format);
|
|
313
316
|
// console.log("测试", arr);
|
|
314
317
|
for (var i = 0; i < arr.length; i++) {
|
|
315
318
|
var key = arr[i];
|
|
@@ -323,11 +326,13 @@ Matchs.prototype.get = function(str, format) {
|
|
|
323
326
|
}
|
|
324
327
|
}
|
|
325
328
|
}
|
|
329
|
+
// console.log("这啥3?", format);
|
|
326
330
|
// console.log("正则", format);
|
|
327
331
|
var txt = "";
|
|
328
332
|
if (format) {
|
|
329
333
|
try {
|
|
330
|
-
format = format.replace(/\]\+\{/g, "]{")
|
|
334
|
+
format = format.replace(/\]\+\{/g, "]{");
|
|
335
|
+
// console.log("这啥4?", format);
|
|
331
336
|
rx = eval('/' + format + '/');
|
|
332
337
|
var mh = str.match(rx);
|
|
333
338
|
if (mh) {
|
|
@@ -367,7 +372,7 @@ String.prototype.matchs = function(format) {
|
|
|
367
372
|
var rx;
|
|
368
373
|
var ret = "";
|
|
369
374
|
var str = this + "";
|
|
370
|
-
if (format.startWith('/')) {
|
|
375
|
+
if (format.startWith('/') && (format.endWith('/') || format.endWith('/g') || format.endWith('/gi'))) {
|
|
371
376
|
try {
|
|
372
377
|
rx = eval(format);
|
|
373
378
|
var mh = str.match(rx);
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -3,175 +3,211 @@ const Matchs = require('./index.js');
|
|
|
3
3
|
async function test() {
|
|
4
4
|
const ms = new Matchs();
|
|
5
5
|
let str = "";
|
|
6
|
-
//
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
//
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
//
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
//
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
6
|
+
// 基础匹配测试 OK
|
|
7
|
+
console.log('=== 基础匹配测试 ===')
|
|
8
|
+
str = '职位不对的';
|
|
9
|
+
console.log(str.matchs("*{不对}"));
|
|
10
|
+
|
|
11
|
+
// 数字正则匹配测试 OK
|
|
12
|
+
console.log('\n=== 数字匹配测试 ===')
|
|
13
|
+
console.log("今年28岁了".matchs('/[0-9]+/'));
|
|
14
|
+
console.log("手机号码是15817188866,哦5646546".matchs('/[0-9]+/g'));
|
|
15
|
+
|
|
16
|
+
// 通配符匹配测试 OK
|
|
17
|
+
console.log('\n=== 通配符匹配测试 ===')
|
|
18
|
+
str = "我叫文武, 手机号码是1581718881";
|
|
19
|
+
console.log(str.matchs('手机号码*'));
|
|
20
|
+
console.log(str.matchs('*武'));
|
|
21
|
+
|
|
22
|
+
str = "因为如果它是一只鸟,那么它就会飞";
|
|
23
|
+
console.log(str.matchs('它*'));
|
|
24
|
+
console.log(str.matchs('如果*鸟'));
|
|
25
|
+
console.log(str.matchs('如果*就*'));
|
|
26
|
+
console.log(str.matchs('*它*就*'));
|
|
27
|
+
|
|
28
|
+
// 职位匹配测试 OK
|
|
29
|
+
console.log('\n=== 职位匹配测试 ===')
|
|
30
|
+
const jobTests = [
|
|
31
|
+
"酒店厨师长",
|
|
32
|
+
"宠物美容师",
|
|
33
|
+
"广告设计师",
|
|
34
|
+
"游戏设计总监",
|
|
35
|
+
"软件开发工程师"
|
|
36
|
+
];
|
|
37
|
+
jobTests.forEach(job => console.log(job.matchs('{职位}')));
|
|
38
|
+
console.log("游戏设计师".matchs('{中文}'));
|
|
39
|
+
console.log("游戏设计师".matchs('{职位}'));
|
|
40
|
+
console.log("游戏设计师".matchs('{中文}{职位}'));
|
|
41
|
+
console.log("游戏设计师".matchs('[\u4e00-\u9fa5]{2,4}{职位}'));
|
|
42
|
+
console.log("广告设计实习生".matchs('[\u4e00-\u9fa5]{2,4}{职位}'));
|
|
43
|
+
console.log("广告设计学徒".matchs('{中文}{2,4}{职位}'));
|
|
44
44
|
|
|
45
45
|
// 日期时间测试
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
//
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
//
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
//
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
//
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
//
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
//
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
//
|
|
46
|
+
console.log('\n=== 日期时间测试 ===')
|
|
47
|
+
str = "今天不是我的生日, 我是一九九一年四月一日生的,生日是4月1日";
|
|
48
|
+
console.log(str.matchs('{时态}'));
|
|
49
|
+
console.log(str.matchs('{日期}'));
|
|
50
|
+
console.log(str.matchs('{年月日}'));
|
|
51
|
+
console.log(str.matchs('{月日}'));
|
|
52
|
+
|
|
53
|
+
str = "1991-04-01 12:15:36";
|
|
54
|
+
console.log(str.matchs('{年月日}'));
|
|
55
|
+
console.log(str.matchs('{月日}'));
|
|
56
|
+
console.log(str.matchs('{日期时间}'));
|
|
57
|
+
console.log(str.matchs('{时分}'));
|
|
58
|
+
console.log(str.matchs('{时分秒}'));
|
|
59
|
+
|
|
60
|
+
str = "1991年4月1日,小宝宝诞生了";
|
|
61
|
+
console.log(str.matchs('{年月日}'));
|
|
62
|
+
console.log(str.matchs('{月日}'));
|
|
63
|
+
|
|
64
|
+
str = "1991年04月01日 12时15分36秒";
|
|
65
|
+
console.log(str.matchs('{年月日}'));
|
|
66
|
+
console.log(str.matchs('{月日}'));
|
|
67
|
+
console.log(str.matchs('{日期时间}'));
|
|
68
|
+
console.log(str.matchs('{日期}'));
|
|
69
|
+
console.log(str.matchs('{时间}'));
|
|
70
|
+
console.log(str.matchs('{日期} {时间}'));
|
|
71
|
+
console.log(str.matchs('{时分}'));
|
|
72
|
+
console.log(str.matchs('{时分秒}'));
|
|
73
|
+
|
|
74
|
+
// 周期匹配测试OK
|
|
75
|
+
console.log('\n=== 周期匹配测试 ===')
|
|
76
|
+
const cycleTests = [
|
|
77
|
+
"十五天内",
|
|
78
|
+
"10-15天左右",
|
|
79
|
+
"十到十五天内",
|
|
80
|
+
"10至15天内",
|
|
81
|
+
"15号之前",
|
|
82
|
+
"数日内",
|
|
83
|
+
"几个小时后",
|
|
84
|
+
"从现在起几个小时之内"
|
|
85
|
+
];
|
|
86
|
+
cycleTests.forEach(cycle => console.log(cycle.matchs('{周期}')));
|
|
87
|
+
|
|
88
|
+
// 手机号码和数字测试 OK
|
|
89
|
+
console.log('\n=== 手机号码和数字测试 ===')
|
|
90
|
+
str = "我叫文武, 手机号码是15817188866";
|
|
91
|
+
console.log(str.matchs('{手机号码}'));
|
|
92
|
+
console.log(str.matchs('{数字}'));
|
|
93
|
+
|
|
94
|
+
// 时间格式测试 OK
|
|
95
|
+
console.log('\n=== 时间格式测试 ===')
|
|
96
|
+
const timeTests = [
|
|
97
|
+
"现在的时间是十九点三十五分五十九秒",
|
|
98
|
+
"现在的时间是19点35分59秒",
|
|
99
|
+
"现在的时间是19:35:59"
|
|
100
|
+
];
|
|
101
|
+
timeTests.forEach(time => {
|
|
102
|
+
console.log(time.matchs('{时间}'));
|
|
103
|
+
console.log(time.matchs('{时分秒}'));
|
|
104
|
+
console.log(time.matchs('{时分}'));
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// 其他格式测试 OK
|
|
108
|
+
console.log('\n=== 其他格式测试 ===')
|
|
109
|
+
const otherTests = [{
|
|
110
|
+
str: "星期天去哪玩?",
|
|
111
|
+
pattern: '{星期}'
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
str: "http://www.elins.cn",
|
|
115
|
+
pattern: '{网址}'
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
str: "573242395@qq.com",
|
|
119
|
+
pattern: '{邮箱}'
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
str: "身份证号441423199104018012",
|
|
123
|
+
pattern: '{身份证号}'
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
str: "QQ号是573242395",
|
|
127
|
+
pattern: '{QQ}'
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
str: "my name is qww, QQ号是573242395",
|
|
131
|
+
pattern: '{中英文}'
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
str: "我的名字叫文武qww",
|
|
135
|
+
pattern: '{中文或英文}'
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
str: "Hello World!",
|
|
139
|
+
pattern: '{中英文}'
|
|
140
|
+
},
|
|
141
|
+
];
|
|
142
|
+
otherTests.forEach(test => console.log(test.str.matchs(test.pattern)));
|
|
143
|
+
|
|
144
|
+
// HTML标签测试 OK
|
|
145
|
+
console.log('\n=== HTML标签测试 ===')
|
|
146
|
+
str = '<img src="http://www.elins.cn/logo.png" />';
|
|
147
|
+
console.log(str.matchs('{img标签}'));
|
|
148
|
+
console.log(str.matchs('{网址}'));
|
|
149
|
+
console.log(str.matchs('{域名}'));
|
|
150
|
+
str = `这是一个链接<a href="http://www.elins.cn/logo.png">
|
|
151
|
+
<div class="test">你好吗?</div>
|
|
152
|
+
</a>`;
|
|
153
|
+
console.log(str.matchs('{链接}'));
|
|
154
|
+
console.log(str.matchs('{html标签}'));
|
|
155
|
+
console.log(str.matchs('{标签之间}'));
|
|
156
|
+
|
|
157
|
+
// 地理位置测试OK
|
|
158
|
+
console.log('\n=== 地理位置测试 ===')
|
|
159
|
+
str = "广东省深圳市南山区粤海街道在中国南方";
|
|
160
|
+
console.log(str.matchs('{省份}'));
|
|
161
|
+
console.log(str.matchs('{城市}'));
|
|
162
|
+
console.log(str.matchs('{管辖区}'));
|
|
163
|
+
console.log(str.matchs('{方位}'));
|
|
164
|
+
console.log(str.matchs('在*{方位}'));
|
|
165
|
+
console.log(str.matchs('在{国家}'));
|
|
166
|
+
|
|
167
|
+
// 车牌和数字测试OK
|
|
168
|
+
console.log('\n=== 车牌和数字测试 ===')
|
|
169
|
+
console.log("我乘坐的车的车牌号是粤B12312".matchs('{车牌号}'));
|
|
170
|
+
console.log("圆周率是3.1415926".matchs('{浮点数}'));
|
|
171
|
+
console.log("圆周率是3.1415926".matchs('{数值}'));
|
|
172
|
+
console.log("圆周率是3.1415926".matchs('{数字}'));
|
|
173
|
+
|
|
174
|
+
// 疑问句测试OK
|
|
175
|
+
console.log('\n=== 疑问句测试 ===')
|
|
176
|
+
const questionTests = [
|
|
177
|
+
"做到了什么程度?",
|
|
178
|
+
"买了什么东西?",
|
|
179
|
+
"摸到了什么东西?",
|
|
180
|
+
"获得了什么东西?",
|
|
181
|
+
"想到了什么?",
|
|
182
|
+
"今天天气怎么样?",
|
|
183
|
+
"听见什么没?",
|
|
184
|
+
"你到哪里了?",
|
|
185
|
+
"买到了没?"
|
|
186
|
+
];
|
|
187
|
+
questionTests.forEach(q => console.log(q.matchs('{疑问}')));
|
|
188
|
+
|
|
189
|
+
// 颜色测试OK
|
|
190
|
+
console.log('\n=== 颜色测试 ===')
|
|
191
|
+
const colorTests = ["紫红色", "朱红色", "大红大紫", "深红色", "浅绿色", "墨黑色"];
|
|
192
|
+
colorTests.forEach(color => console.log(color.matchs('{颜色}')));
|
|
193
|
+
|
|
194
|
+
// 年龄测试OK
|
|
195
|
+
console.log('\n=== 年龄测试 ===')
|
|
196
|
+
const ageTests = ["年方二八", "28岁了", "十八岁了"];
|
|
197
|
+
ageTests.forEach(age => console.log(age.matchs('{年龄}')));
|
|
198
|
+
|
|
199
|
+
str = "/user";
|
|
200
|
+
console.log(str.matchs("/user"));
|
|
201
|
+
|
|
202
|
+
str = "/user/1";
|
|
203
|
+
console.log(str.matchs("/user:id"));
|
|
204
|
+
|
|
205
|
+
str = "/user/1";
|
|
206
|
+
console.log(str.matchs("/user/*"));
|
|
207
|
+
|
|
208
|
+
str = "/user/1";
|
|
209
|
+
console.log(str.matchs("/user/"));
|
|
210
|
+
|
|
175
211
|
}
|
|
176
212
|
|
|
177
213
|
test();
|