回答編集履歴

5

修正

2021/09/05 06:56

投稿

Zuishin
Zuishin

スコア28662

test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
 
10
10
 
11
- const dst = src.replaceAll(/山梨県|梨/g, a => a === "梨" ? "りんご" : a);
11
+ const dst = src.replaceAll(/山梨県|梨/gu, a => a === "梨" ? "りんご" : a);
12
12
 
13
13
  console.log(dst);
14
14
 
@@ -22,7 +22,7 @@
22
22
 
23
23
  ```js
24
24
 
25
- const dst = src.replaceAll(/山梨県|二十世紀梨|梨/g, a => a === "梨" ? "りんご" : a);
25
+ const dst = src.replaceAll(/山梨県|二十世紀梨|梨/gu, a => a === "梨" ? "りんご" : a);
26
26
 
27
27
  ```
28
28
 
@@ -46,7 +46,7 @@
46
46
 
47
47
  const searchValue = ignore.concat(target).map(escapeRegularExpression).join("|");
48
48
 
49
- const regExp = new RegExp(searchValue, "g");
49
+ const regExp = new RegExp(searchValue, "gu");
50
50
 
51
51
  return src.replace(regExp, a => a === target ? replacement : a);
52
52
 
@@ -59,3 +59,19 @@
59
59
  console.log(dst);
60
60
 
61
61
  ```
62
+
63
+
64
+
65
+ target や ignore にメタ文字が含まれておらず、無視する単語が一つということがどちらも保証されているなら、次のように簡単に書いてもいいでしょう。
66
+
67
+
68
+
69
+ ```js
70
+
71
+ const f = (src, target, ignore, replacement) => {
72
+
73
+ return src.replace(new RegExp(`${ignore}|${target}`, "gu"), a => a === target ? replacement : a);
74
+
75
+ };
76
+
77
+ ```

4

リファクタリング

2021/09/05 06:56

投稿

Zuishin
Zuishin

スコア28662

test CHANGED
@@ -44,7 +44,7 @@
44
44
 
45
45
  const f = (src, target, ignore, replacement) => {
46
46
 
47
- const searchValue = ignore.concat(target).map(a => escapeRegularExpression(a)).join("|");
47
+ const searchValue = ignore.concat(target).map(escapeRegularExpression).join("|");
48
48
 
49
49
  const regExp = new RegExp(searchValue, "g");
50
50
 

3

リファクタリング

2021/09/05 04:49

投稿

Zuishin
Zuishin

スコア28662

test CHANGED
@@ -44,9 +44,7 @@
44
44
 
45
45
  const f = (src, target, ignore, replacement) => {
46
46
 
47
- const targetValue = escapeRegularExpression(target);
48
-
49
- const searchValue = ignore.reduce((previousValue, currentValue) => `${escapeRegularExpression(currentValue)}|${previousValue}`, targetValue);
47
+ const searchValue = ignore.concat(target).map(a => escapeRegularExpression(a)).join("|");
50
48
 
51
49
  const regExp = new RegExp(searchValue, "g");
52
50
 

2

修正

2021/09/05 04:46

投稿

Zuishin
Zuishin

スコア28662

test CHANGED
@@ -50,7 +50,7 @@
50
50
 
51
51
  const regExp = new RegExp(searchValue, "g");
52
52
 
53
- return src.replace(regExp, a => a === targetValue ? replacement : a);
53
+ return src.replace(regExp, a => a === target ? replacement : a);
54
54
 
55
55
  };
56
56
 

1

追記

2021/09/05 04:16

投稿

Zuishin
Zuishin

スコア28662

test CHANGED
@@ -25,3 +25,39 @@
25
25
  const dst = src.replaceAll(/山梨県|二十世紀梨|梨/g, a => a === "梨" ? "りんご" : a);
26
26
 
27
27
  ```
28
+
29
+
30
+
31
+ 以上を関数化すると次のようになります。
32
+
33
+
34
+
35
+ ```js
36
+
37
+ const escapeRegularExpression = re => {
38
+
39
+ return re.replace(/[\^$*+?.(){[|]/g, a => `\${a}`);
40
+
41
+ };
42
+
43
+
44
+
45
+ const f = (src, target, ignore, replacement) => {
46
+
47
+ const targetValue = escapeRegularExpression(target);
48
+
49
+ const searchValue = ignore.reduce((previousValue, currentValue) => `${escapeRegularExpression(currentValue)}|${previousValue}`, targetValue);
50
+
51
+ const regExp = new RegExp(searchValue, "g");
52
+
53
+ return src.replace(regExp, a => a === targetValue ? replacement : a);
54
+
55
+ };
56
+
57
+
58
+
59
+ const dst = f("特に山梨県の梨はみずみずしくて美味い", "梨", ["山梨県"], "りんご");
60
+
61
+ console.log(dst);
62
+
63
+ ```