mm_matchs 1.1.4 → 1.1.5

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/README.md CHANGED
@@ -1,2 +1,125 @@
1
- # mm_matchs
2
- 这是用来匹配复杂的字符串结构的函数,可以匹配前缀包含、后缀包含、以及时间、地点等
1
+ # mm_matchs
2
+
3
+ 这是一个用于匹配复杂字符串结构的Node.js模块,提供了丰富的字符串匹配功能,可以匹配前缀包含、后缀包含、以及时间、地点等多种模式。
4
+
5
+ ## 特点
6
+
7
+ - 支持多种匹配模式:前缀匹配、后缀匹配、正则匹配等
8
+ - 内置丰富的匹配规则:时间、日期、地点、网址等
9
+ - 使用简单,支持链式调用
10
+ - 可扩展性强,支持自定义匹配规则
11
+
12
+ ## 安装
13
+
14
+ ```bash
15
+ npm install mm_matchs
16
+ ```
17
+
18
+ ## 基础用法
19
+
20
+ ```javascript
21
+ const Matchs = require('mm_matchs');
22
+ const ms = new Matchs();
23
+
24
+ // 使用String原型扩展方法
25
+ const str = "我叫文武, 手机号码是15817188866";
26
+
27
+ // 前缀匹配
28
+ console.log(str.matchs('手机号码*')); // 15817188866
29
+
30
+ // 后缀匹配
31
+ console.log(str.matchs('*武')); // 文武
32
+
33
+ // 区间匹配
34
+ console.log(str.matchs('如果*就*')); // 匹配两个通配符之间的内容
35
+ ```
36
+
37
+ ## 内置匹配规则
38
+
39
+ ### 时间日期类
40
+
41
+ ```javascript
42
+ // 匹配日期时间
43
+ str = "1991-04-01 12:15:36";
44
+ console.log(str.matchs('{日期时间}'));
45
+
46
+ // 匹配年月日
47
+ str = "1991年4月1日";
48
+ console.log(str.matchs('{年月日}'));
49
+
50
+ // 匹配时间
51
+ str = "现在的时间是19:35:59";
52
+ console.log(str.matchs('{时间}'));
53
+ ```
54
+
55
+ ### 地理位置类
56
+
57
+ ```javascript
58
+ // 匹配省份
59
+ str = "广东省深圳市南山区";
60
+ console.log(str.matchs('{省份}'));
61
+
62
+ // 匹配城市
63
+ console.log(str.matchs('{城市}'));
64
+
65
+ // 匹配方位
66
+ console.log(str.matchs('{方位}'));
67
+ ```
68
+
69
+ ### 网络通信类
70
+
71
+ ```javascript
72
+ // 匹配网址
73
+ console.log("http://www.example.com".matchs('{网址}'));
74
+
75
+ // 匹配邮箱
76
+ console.log("test@example.com".matchs('{邮箱}'));
77
+
78
+ // 匹配手机号码
79
+ console.log("13812345678".matchs('{手机号码}'));
80
+ ```
81
+
82
+ ### 其他常用匹配
83
+
84
+ ```javascript
85
+ // 匹配身份证号
86
+ console.log("440123199001011234".matchs('{身份证号}'));
87
+
88
+ // 匹配车牌号
89
+ console.log("粤B12345".matchs('{车牌号}'));
90
+
91
+ // 匹配中英文
92
+ console.log("Hello世界".matchs('{中英文}'));
93
+ ```
94
+
95
+ ## API文档
96
+
97
+ ### 主要方法
98
+
99
+ - `matchs(pattern)`: 字符串匹配方法,支持通配符和预定义规则
100
+ - `tense(str, format)`: 匹配时态
101
+ - `bearing(str, format)`: 匹配方位
102
+ - `province(str, format)`: 匹配省份
103
+ - `city(str, format)`: 匹配城市
104
+
105
+ ### 预定义规则
106
+
107
+ 模块内置了多种匹配规则,使用花括号`{}`包裹规则名称:
108
+
109
+ - `{日期时间}`: 匹配日期时间格式
110
+ - `{日期}`: 匹配日期
111
+ - `{时间}`: 匹配时间
112
+ - `{网址}`: 匹配URL
113
+ - `{邮箱}`: 匹配电子邮箱
114
+ - `{手机号码}`: 匹配手机号
115
+ - `{身份证号}`: 匹配身份证号码
116
+ - `{车牌号}`: 匹配车牌号
117
+ - `{中英文}`: 匹配中英文混合
118
+
119
+ ## 许可证
120
+
121
+ ISC License
122
+
123
+ ## 作者
124
+
125
+ qww
package/html.js CHANGED
@@ -11,18 +11,21 @@ class Html2Article {
11
11
  * @param {Object} arg
12
12
  */
13
13
  constructor(arg) {
14
+ // 导入正则表达式
15
+ this.regex = require('./regex');
16
+
14
17
  // 正则表达式过滤:正则表达式,要替换成的文本
15
18
  this.filters = [{
16
- rx: "/<script.*?>.*?<\\/script>/g",
19
+ rx: this.regex.script,
17
20
  value: ""
18
21
  }, {
19
- rx: "/<style.*?>.*?<\\/style>/g",
22
+ rx: this.regex.style,
20
23
  value: ""
21
24
  }, {
22
- rx: "/<!--.*?-->/g",
25
+ rx: this.regex.comment,
23
26
  value: ""
24
27
  }, {
25
- rx: "/<\\/a>/g",
28
+ rx: this.regex.link,
26
29
  value: "</a>\n"
27
30
  }];
28
31
 
@@ -71,19 +74,19 @@ Html2Article.prototype.getArticle = function(html) {
71
74
  var body = "";
72
75
 
73
76
  if (html.indexOf("<article") !== -1) {
74
- var mh = html.match(/<article[\s\S]*<\/article>/gi);
77
+ var mh = html.match(eval(this.regex.article));
75
78
  if (mh) {
76
79
  body = mh[0];
77
80
  }
78
81
  } else {
79
82
  if (html.indexOf("<main") !== -1) {
80
- var mh = html.match(/<main[\s\S]*<\/main>/gi);
83
+ var mh = html.match(eval(this.regex.main));
81
84
  if (mh) {
82
85
  body = mh[0];
83
86
  }
84
87
  }
85
88
  else {
86
- var mh = html.match(/<body[\s\S]*<\/body>/gi);
89
+ var mh = html.match(eval(this.regex.body));
87
90
  if (mh) {
88
91
  body = mh[0];
89
92
  }
@@ -146,10 +149,8 @@ Html2Article.prototype.getTitle = function(html) {
146
149
  * @return {String} 返回时间字符串
147
150
  */
148
151
  Html2Article.prototype.getPublishDate = function(html) {
149
- // 过滤html标签,防止标签对日期提取产生影响
150
- var text = html.replace(/<.*?>/g, "");
151
- var rx =
152
- /((\d{4}(\-|\/|\.)(0[1-9]|1[012]|[1-9])(\-|\/|\.)([12][0-9]|0[1-9]|3[01]|[1-9]))|(([一二三四五六七八九]+年)?[一二三四五六七八九十]+月([一二三四五六七八九十]+(日|号))?)|((\d{4}年)?(0?[1-9]|1[012]|[1-9])月([12][0-9]|0?[1-9]|3[01]|[1-9])(日|号))) ?((([01][0-9]|2[0-3]):([0-4][0-9]|5[0-9])(:([0-4][0-9]|5[0-9]))?)|(([01][0-9]|2[0-3])点([0-4][0-9]|5[0-9])分(([0-4][0-9]|5[0-9])秒)?)|([一二三四五六七八九十]+点[一二三四五六七八九十]+分([一二三四五六七八九十]+秒)?))/;
152
+ var text = html.replace(eval(this.regex.clear_tag), "");
153
+ var rx = eval(this.regex.datetime);
153
154
  var mh = text.match(rx);
154
155
  var result = "1900-01-01";
155
156
  if (mh) {
@@ -175,8 +176,7 @@ Html2Article.prototype.getContent = function(bodyText) {
175
176
  // 去除每行的空白字符,剔除标签
176
177
  for (var i = 0; i < orgLines.length; i++) {
177
178
  var lineInfo = orgLines[i];
178
- // 处理回车,使用[crlf]做为回车标记符,最后统一处理
179
- var str = lineInfo.replace(/<\/p>|<br.*?\/>/gi, "\r\n").replace(/<.*?>/g, "").trim();
179
+ var str = lineInfo.replace(eval(this.regex.line_break), "\r\n").replace(eval(this.regex.clearTag), "").trim();
180
180
  if (str) {
181
181
  lines.push(str);
182
182
  }
package/htmlformat.js CHANGED
@@ -75,7 +75,7 @@ function style_html(html_source, indent_size, indent_character, max_char) {
75
75
 
76
76
  var char = '';
77
77
  var content = [];
78
- var reg_match = new RegExp('\<\/script' + '\>', 'igm');
78
+ var reg_match = this.regex.scriptEndTag;
79
79
  reg_match.lastIndex = this.pos;
80
80
  var reg_array = reg_match.exec(this.input);
81
81
  var end_script = reg_array ? reg_array.index : this.input.length; //absolute end of script
@@ -193,7 +193,7 @@ function style_html(html_source, indent_size, indent_character, max_char) {
193
193
  } else if (tag_check.charAt(0) === '!') { //peek for <!-- comment
194
194
  if (tag_check.indexOf('[if') != -1) { //peek for <!--[if conditional comment
195
195
  if (tag_complete.indexOf('!IE') != -1) { //this type needs a closing --> so...
196
- var comment = this.get_unformatted('-->', tag_complete); //...delegate to get_unformatted
196
+ var comment = this.get_unformatted(this.regex.commentEnd, tag_complete); //...delegate to get_unformatted
197
197
  content.push(comment);
198
198
  }
199
199
  this.tag_type = 'START';
@@ -201,7 +201,7 @@ function style_html(html_source, indent_size, indent_character, max_char) {
201
201
  this.tag_type = 'END';
202
202
  this.unindent();
203
203
  } else if (tag_check.indexOf('[cdata[') != -1) { //if it's a <[cdata[ comment...
204
- var comment = this.get_unformatted(']]>', tag_complete); //...delegate to get_unformatted function
204
+ var comment = this.get_unformatted(this.regex.cdataEnd, tag_complete); //...delegate to get_unformatted function
205
205
  content.push(comment);
206
206
  this.tag_type = 'SINGLE'; //<![CDATA[ comments are treated like single tags
207
207
  } else {