回答編集履歴

1

参考ソース追加

2017/01/27 15:37

投稿

退会済みユーザー
test CHANGED
@@ -45,3 +45,91 @@
45
45
  text = text.replace(/<span class="my_color">\s+<\/span>/g,'');
46
46
 
47
47
  ```
48
+
49
+ ---
50
+
51
+ 追記) 0:36 参考までにフルソース
52
+
53
+ ```html
54
+
55
+ <html>
56
+
57
+ <script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
58
+
59
+ <script>
60
+
61
+ $(function() {
62
+
63
+ $("#change").on("click",function() {
64
+
65
+ var text = $("#request").val();
66
+
67
+ //タグとタグの間の文字列を span で囲む
68
+
69
+ text = text.replace(/(<(?!ruby|rt|rp)[^<]*?>)([^<\n]+)(<[^<]*?>)/g,'$1<span class="my_color">$2</span>$3');
70
+
71
+
72
+
73
+ //文頭から最初のタグまでの文字列を span で囲む
74
+
75
+ text = text.replace(/^([^<]+?)</gm,'<span class="my_color">$1</span><');
76
+
77
+
78
+
79
+ //最後のタグから文末までの文字列を span で囲む
80
+
81
+ text = text.replace(/>([^>]+)$/g,'><span class="my_color">$1</span>');
82
+
83
+
84
+
85
+ // タグの狭間のくくりを追加
86
+
87
+ text = text.replace(/(<\/[^>]+>)([^<]+)(<[^>]+?>)/g,'$1<span class="my_color">$2</span>$3');
88
+
89
+
90
+
91
+ // ダサいスペースだけのタグくくりを排除
92
+
93
+ text = text.replace(/<span class="my_color">\s+<\/span>/g,'');
94
+
95
+
96
+
97
+ $("#result").val(text);
98
+
99
+ });
100
+
101
+ });
102
+
103
+ </script>
104
+
105
+ <body>
106
+
107
+ <textarea id="request" style="width:800;height:160;">
108
+
109
+ 例によって金田邸へ忍び込む。<br>
110
+
111
+ <em class="sesame_dot">例によって</em>とは<ruby><rb>今更</rb><rp>(</rp><rt>いまさら</rt><rp>)</rp></ruby>解釈する必要もない。
112
+
113
+ </textarea>
114
+
115
+ <br>
116
+
117
+ <input type="button" value="変換" id="change"><br>
118
+
119
+ <textarea id="result" style="width:800;height:160;">
120
+
121
+ </textarea>
122
+
123
+ <textarea id="answer" style="width:800;height:160;">
124
+
125
+ <span class="my_color">例によって金田邸へ忍び込む。</span><br>
126
+
127
+ <em class="sesame_dot"><span class="my_color">例によって</span></em><span class="my_color">とは</span><ruby><rb><span class="my_color">今更</span></rb><rp>(</rp><rt>いまさら</rt><rp>)</rp></ruby><span class="my_color">解釈する必要もない。</span>
128
+
129
+ </textarea>
130
+
131
+ </body>
132
+
133
+ </html>
134
+
135
+ ```