回答編集履歴
4
a
test
CHANGED
@@ -109,3 +109,45 @@
|
|
109
109
|
|
110
110
|
|
111
111
|
```
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
# 追記
|
116
|
+
|
117
|
+
```
|
118
|
+
|
119
|
+
const pattern = new RegExp('a');
|
120
|
+
|
121
|
+
const str = 'a';
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
console.log(typeof pattern) // object
|
126
|
+
|
127
|
+
console.log(typeof str) // string
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
console.log(pattern.match) // undefined
|
132
|
+
|
133
|
+
console.log(str.match) // [Function: match]
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
// TypeError: pattern.match is not a function
|
138
|
+
|
139
|
+
// console.log(pattern.match(str));
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
// [ 'a', index: 0, input: 'a' ]
|
144
|
+
|
145
|
+
console.log(str.match(pattern));
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
```
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
この追加のコードと、元の回答にある説明を読んで、何が原因でエラーが起きているのかが分かると良いのですが。
|
3
a
test
CHANGED
@@ -80,12 +80,6 @@
|
|
80
80
|
|
81
81
|
```ここに言語を入力
|
82
82
|
|
83
|
-
const target = { name: 'Cancel Button' };
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
const pattern = new RegExp('Cancel');
|
88
|
-
|
89
83
|
|
90
84
|
|
91
85
|
const target = { name: 'Cancel Button' };
|
2
a
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
const pattern = new RegExp('Cacel');
|
9
|
+
const pattern = new RegExp('Cancel');
|
10
10
|
|
11
11
|
|
12
12
|
|
@@ -74,7 +74,7 @@
|
|
74
74
|
|
75
75
|
|
76
76
|
|
77
|
-
# 解決策
|
77
|
+
# 解決策の一例
|
78
78
|
|
79
79
|
|
80
80
|
|
@@ -88,9 +88,29 @@
|
|
88
88
|
|
89
89
|
|
90
90
|
|
91
|
+
const target = { name: 'Cancel Button' };
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
const pattern = new RegExp('Cancel');
|
96
|
+
|
97
|
+
|
98
|
+
|
91
99
|
// [ 'Cancel', index: 0, input: 'Cancel Button' ]
|
92
100
|
|
93
|
-
target.name.match(pattern)
|
101
|
+
console.log(target.name.match(pattern))
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
// false
|
106
|
+
|
107
|
+
console.log(pattern.test(target.name))
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
// [ 'Cancel', index: 0, input: 'Cancel Button' ]
|
112
|
+
|
113
|
+
console.log(pattern.exec(target.name))
|
94
114
|
|
95
115
|
|
96
116
|
|
1
a
test
CHANGED
@@ -54,6 +54,22 @@
|
|
54
54
|
|
55
55
|
|
56
56
|
|
57
|
+
では、`match`メソッドというのはJavaScriptに存在しないのかというと、そういうわけでもなく、`match`メソッドは`String`型のオブジェクトのメソッドとして存在しています。
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
> str.match(regexp)
|
62
|
+
|
63
|
+
> 正規表現に対する文字列 のマッチングの際に、そのマッチ結果を得るために使われます。
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
[https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/match](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/match)
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
ということで、原因は以上です。
|
72
|
+
|
57
73
|
|
58
74
|
|
59
75
|
|