回答編集履歴
5
追記
answer
CHANGED
@@ -41,6 +41,6 @@
|
|
41
41
|
const prefixChars = ['、', '。'];
|
42
42
|
const reg = new RegExp(`([ \s${prefixChars.join('')}])([##][^\s ]+)`, 'g'); // 1:53修正→Sep 25 18:55再編集
|
43
43
|
const replaced = haystack.replace(reg, (match, prefix, tagging) => `${prefix}<span class="tag">${tagging}</span>`);
|
44
|
-
$('#clone').html(replaced.substring(1, replaced.length -
|
44
|
+
$('#clone').html(replaced.substring(1, replaced.length - 1)); // Sep 26, 2:51編集
|
45
45
|
}
|
46
46
|
```
|
4
エスケープ処理に間違いがあったのを修正
answer
CHANGED
@@ -39,7 +39,7 @@
|
|
39
39
|
|
40
40
|
// タグの開始直前に許容する記号の一覧
|
41
41
|
const prefixChars = ['、', '。'];
|
42
|
-
const reg = new RegExp(`([ \s${prefixChars.join('')}])([##][^\s ]+)`, 'g'); // 1:53修正
|
42
|
+
const reg = new RegExp(`([ \s${prefixChars.join('')}])([##][^\s ]+)`, 'g'); // 1:53修正→Sep 25 18:55再編集
|
43
43
|
const replaced = haystack.replace(reg, (match, prefix, tagging) => `${prefix}<span class="tag">${tagging}</span>`);
|
44
44
|
$('#clone').html(replaced.substring(1, replaced.length - 2));
|
45
45
|
}
|
3
コメントに対する追記に対するコメントに対する追記の修正
answer
CHANGED
@@ -39,7 +39,7 @@
|
|
39
39
|
|
40
40
|
// タグの開始直前に許容する記号の一覧
|
41
41
|
const prefixChars = ['、', '。'];
|
42
|
-
const reg = new RegExp(`([ \s${prefixChars.join('')}])([##][
|
42
|
+
const reg = new RegExp(`([ \s${prefixChars.join('')}])([##][^\s ]+)`, 'g'); // 1:53修正
|
43
43
|
const replaced = haystack.replace(reg, (match, prefix, tagging) => `${prefix}<span class="tag">${tagging}</span>`);
|
44
44
|
$('#clone').html(replaced.substring(1, replaced.length - 2));
|
45
45
|
}
|
2
コメントに対する追記に対するコメントに対する追記
answer
CHANGED
@@ -20,4 +20,27 @@
|
|
20
20
|
----
|
21
21
|
Sep 21, 2019 19:00追記
|
22
22
|
出先なのでかんたんに画像で恐縮ですが、\rと\nを条件に出したら改行のところも動きませんでしょうか。
|
23
|
-

|
23
|
+

|
24
|
+
|
25
|
+
----
|
26
|
+
Sep 22, 2019 1:10追記
|
27
|
+
> なぜか「#we」だけがタグ認定されない事態に…
|
28
|
+
|
29
|
+
なるほどーーーーー、
|
30
|
+
` #yes改行#we改行#can ` に対して「前後に改行空白を持つ部分」で取っているので、まず1つ目で「 #yes改行」が取られて、次の#weは前に改行を持っていない(yesに取られた)状態になって、というような感じで、偶数個めが認識されなくなりますね。
|
31
|
+
仕様を満たすかどうかは要検証ですがこちらに変えると、ご提示の例では動くかと……
|
32
|
+
|
33
|
+
```js
|
34
|
+
function hash(str) {
|
35
|
+
// ・文字列がtextarea先頭の場合、直前のスペースは不要
|
36
|
+
// ・文字列がtextarea末尾の場合、直後のスペースは不要
|
37
|
+
// の処理を簡単にするために前後に半角スペースを入れる
|
38
|
+
const haystack = ` ${str} `;
|
39
|
+
|
40
|
+
// タグの開始直前に許容する記号の一覧
|
41
|
+
const prefixChars = ['、', '。'];
|
42
|
+
const reg = new RegExp(`([ \s${prefixChars.join('')}])([##][ \S]+)`, 'g');
|
43
|
+
const replaced = haystack.replace(reg, (match, prefix, tagging) => `${prefix}<span class="tag">${tagging}</span>`);
|
44
|
+
$('#clone').html(replaced.substring(1, replaced.length - 2));
|
45
|
+
}
|
46
|
+
```
|
1
改行について追記
answer
CHANGED
@@ -15,4 +15,9 @@
|
|
15
15
|
$('#clone').html(replaced.substring(1, replaced.length - 2));
|
16
16
|
}
|
17
17
|
```
|
18
|
-
未検証ですがこんな感じではいかがでしょう
|
18
|
+
未検証ですがこんな感じではいかがでしょう
|
19
|
+
|
20
|
+
----
|
21
|
+
Sep 21, 2019 19:00追記
|
22
|
+
出先なのでかんたんに画像で恐縮ですが、\rと\nを条件に出したら改行のところも動きませんでしょうか。
|
23
|
+

|