teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

4

a

2018/06/22 10:04

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -53,4 +53,25 @@
53
53
  // [ 'Cancel', index: 0, input: 'Cancel Button' ]
54
54
  console.log(pattern.exec(target.name))
55
55
 
56
- ```
56
+ ```
57
+
58
+ # 追記
59
+ ```
60
+ const pattern = new RegExp('a');
61
+ const str = 'a';
62
+
63
+ console.log(typeof pattern) // object
64
+ console.log(typeof str) // string
65
+
66
+ console.log(pattern.match) // undefined
67
+ console.log(str.match) // [Function: match]
68
+
69
+ // TypeError: pattern.match is not a function
70
+ // console.log(pattern.match(str));
71
+
72
+ // [ 'a', index: 0, input: 'a' ]
73
+ console.log(str.match(pattern));
74
+
75
+ ```
76
+
77
+ この追加のコードと、元の回答にある説明を読んで、何が原因でエラーが起きているのかが分かると良いのですが。

3

a

2018/06/22 10:03

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -39,10 +39,7 @@
39
39
  # 解決策の一例
40
40
 
41
41
  ```ここに言語を入力
42
- const target = { name: 'Cancel Button' };
43
42
 
44
- const pattern = new RegExp('Cancel');
45
-
46
43
  const target = { name: 'Cancel Button' };
47
44
 
48
45
  const pattern = new RegExp('Cancel');

2

a

2018/06/22 00:49

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -2,7 +2,7 @@
2
2
  ```ここに言語を入力
3
3
  const target = { name: 'Cancel Button' };
4
4
 
5
- const pattern = new RegExp('Cacel');
5
+ const pattern = new RegExp('Cancel');
6
6
 
7
7
  // TypeError: filter.match is not a function
8
8
  pattern.match(target.name)
@@ -36,14 +36,24 @@
36
36
  ということで、原因は以上です。
37
37
 
38
38
 
39
- # 解決策
39
+ # 解決策の一例
40
40
 
41
41
  ```ここに言語を入力
42
42
  const target = { name: 'Cancel Button' };
43
43
 
44
44
  const pattern = new RegExp('Cancel');
45
45
 
46
+ const target = { name: 'Cancel Button' };
47
+
48
+ const pattern = new RegExp('Cancel');
49
+
46
50
  // [ 'Cancel', index: 0, input: 'Cancel Button' ]
47
- target.name.match(pattern)
51
+ console.log(target.name.match(pattern))
48
52
 
53
+ // false
54
+ console.log(pattern.test(target.name))
55
+
56
+ // [ 'Cancel', index: 0, input: 'Cancel Button' ]
57
+ console.log(pattern.exec(target.name))
58
+
49
59
  ```

1

a

2018/06/21 21:06

投稿

HayatoKamono
HayatoKamono

スコア2415

answer CHANGED
@@ -26,8 +26,16 @@
26
26
  > 特定のオブジェクトを表すオブジェクトリテラルを返します。 この値は新しいオブジェクトを生成するために使うことができます。Object.prototype.toSource メソッドを上書きします。
27
27
  > toString
28
28
 
29
+ では、`match`メソッドというのはJavaScriptに存在しないのかというと、そういうわけでもなく、`match`メソッドは`String`型のオブジェクトのメソッドとして存在しています。
29
30
 
31
+ > str.match(regexp)
32
+ > 正規表現に対する文字列 のマッチングの際に、そのマッチ結果を得るために使われます。
30
33
 
34
+ [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)
35
+
36
+ ということで、原因は以上です。
37
+
38
+
31
39
  # 解決策
32
40
 
33
41
  ```ここに言語を入力