回答編集履歴
1
コメントを受けて追記
test
CHANGED
@@ -2,3 +2,114 @@
|
|
2
2
|
|
3
3
|
カンですが、対象の文字列`新潟県新潟市西蒲区適当八-三十四-三十一`に含まれている`-`(\u002d)が、いずれの正規表現にも含まれていないのが原因ではないでしょうか。
|
4
4
|
|
5
|
+
---
|
6
|
+
|
7
|
+
### コメントを受けて追記
|
8
|
+
|
9
|
+
コアと思われるコードを適当に抜き出して、ES6にして入力をArrayにして`十`を正規表現に加えて実行してみましたが、問題がなさそうです。
|
10
|
+
|
11
|
+
```js
|
12
|
+
maxLength = 21;
|
13
|
+
proposal = {
|
14
|
+
kind: "info",
|
15
|
+
title: "住所を分割しました。",
|
16
|
+
description: "" + maxLength + "文字を越えていたので分割しました。",
|
17
|
+
columns: []
|
18
|
+
};
|
19
|
+
pattern1 = /[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|丁目|-)[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|-|番)[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|-|号)[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|-)[0-90-9〇一二三四五六七八九十]{1,}.*/g;
|
20
|
+
pattern2 = /[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|丁目|-)[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|-|番)[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|-|号)[0-90-9〇一二三四五六七八九十]{1,}.*/g;
|
21
|
+
pattern3 = /[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|丁目|-)[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|-|番)[0-90-9〇一二三四五六七八九十]{1,}.*/g;
|
22
|
+
pattern4 = /[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|丁目|-)[0-90-9〇一二三四五六七八九十]{1,}.*/g;
|
23
|
+
pattern5 = /[0-90-9〇一二三四五六七八九十]{3,}.*/;
|
24
|
+
pattern1_2 = /^[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|丁目|-)[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|-|番)[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|-|号)[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|-)[0-90-9〇一二三四五六七八九十]{1,}/g;
|
25
|
+
pattern2_2 = /^[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|丁目|-)[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|-|番)[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|-|号)[0-90-9〇一二三四五六七八九十]{1,}/g;
|
26
|
+
pattern3_2 = /^[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|丁目|-)[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|-|番)[0-90-9〇一二三四五六七八九十]{1,}/g;
|
27
|
+
pattern4_2 = /^[0-90-9〇一二三四五六七八九十]{1,}(、|.|・|?|||ー|丁目|-)[0-90-9〇一二三四五六七八九十]{1,}/g;
|
28
|
+
pattern5_2 = /^[0-90-9〇一二三四五六七八九十]{3,}/;
|
29
|
+
addressIndex = -1;
|
30
|
+
['新潟県新潟市西蒲区適当八ー三十四ー三十一'].forEach(function(address, addressNext) {
|
31
|
+
var firstLine, matchValue, matchs1, matchs2, matchs3, matchs4, matchs5, moreMatchValue, moreMatchs, newLines, oldLines, secondLine;
|
32
|
+
newLines = [];
|
33
|
+
oldLines = [address];
|
34
|
+
// if (oldLines.length > 0 && oldLines[0].length >= maxLength && (oldLines.length === 1 || (oldLines.length > 1 && oldLines[1].length === 0))) {
|
35
|
+
firstLine = oldLines[0];
|
36
|
+
secondLine = "";
|
37
|
+
matchs1 = firstLine.match(pattern1);
|
38
|
+
matchs2 = firstLine.match(pattern2);
|
39
|
+
matchs3 = firstLine.match(pattern3);
|
40
|
+
matchs4 = firstLine.match(pattern4);
|
41
|
+
matchs5 = firstLine.match(pattern5);
|
42
|
+
matchValue = "";
|
43
|
+
if (matchs1 != null) {
|
44
|
+
matchValue = matchs1.join("");
|
45
|
+
moreMatchs = matchValue.match(pattern1_2);
|
46
|
+
} else if (matchs2 != null) {
|
47
|
+
matchValue = matchs2.join("");
|
48
|
+
moreMatchs = matchValue.match(pattern2_2);
|
49
|
+
} else if (matchs3 != null) {
|
50
|
+
matchValue = matchs3.join("");
|
51
|
+
moreMatchs = matchValue.match(pattern3_2);
|
52
|
+
} else if (matchs4 != null) {
|
53
|
+
matchValue = matchs4.join("");
|
54
|
+
moreMatchs = matchValue.match(pattern4_2);
|
55
|
+
} else if (matchs5 != null) {
|
56
|
+
matchValue = matchs5.join("");
|
57
|
+
moreMatchs = matchValue.match(pattern5_2);
|
58
|
+
}
|
59
|
+
console.log("matchValue: " + matchValue);
|
60
|
+
if (matchValue.length > 0) {
|
61
|
+
firstLine = firstLine.replace(matchValue, "");
|
62
|
+
secondLine = matchValue;
|
63
|
+
if (moreMatchs != null) {
|
64
|
+
moreMatchValue = moreMatchs.join("");
|
65
|
+
console.log("moreMatchValue: " + moreMatchValue);
|
66
|
+
if (matchValue !== moreMatchValue && (matchValue.length - moreMatchValue.length) > 2) {
|
67
|
+
firstLine = firstLine.replace(matchValue, "");
|
68
|
+
firstLine = "" + firstLine + moreMatchValue;
|
69
|
+
secondLine = matchValue.replace(moreMatchValue, "");
|
70
|
+
}
|
71
|
+
}
|
72
|
+
if (!secondLine.match("^(、|.|・|?|||ー|丁目|番地|号|-)")) {
|
73
|
+
newLines.push(firstLine);
|
74
|
+
newLines.push(secondLine);
|
75
|
+
}
|
76
|
+
}
|
77
|
+
if (newLines.length > 0) {
|
78
|
+
address.lines = newLines;
|
79
|
+
proposal.columns.push({
|
80
|
+
paths: [
|
81
|
+
{
|
82
|
+
key: "addresses",
|
83
|
+
index: addressIndex
|
84
|
+
}, {
|
85
|
+
key: "lines",
|
86
|
+
index: 0
|
87
|
+
}
|
88
|
+
],
|
89
|
+
values: {
|
90
|
+
old: oldLines[0],
|
91
|
+
"new": newLines[0]
|
92
|
+
},
|
93
|
+
matchs: []
|
94
|
+
});
|
95
|
+
proposal.columns.push({
|
96
|
+
paths: [
|
97
|
+
{
|
98
|
+
key: "addresses",
|
99
|
+
index: addressIndex
|
100
|
+
}, {
|
101
|
+
key: "lines",
|
102
|
+
index: 1
|
103
|
+
}
|
104
|
+
],
|
105
|
+
values: {
|
106
|
+
old: oldLines[1],
|
107
|
+
"new": newLines[1]
|
108
|
+
},
|
109
|
+
matchs: []
|
110
|
+
});
|
111
|
+
}
|
112
|
+
// }
|
113
|
+
})
|
114
|
+
console.log(proposal);
|
115
|
+
```
|