プログラム書いてみました。ブラウザで開いていただければすぐ使えます。
以下の手順に沿って進めてみてください。
HTML内のコードをテキストエディタにコピペ。
ファイル名をindex.htmlにして保存
ダブルクリックでそのファイルを開く(ブラウザが立ち上がります)
各項目を埋めて「置換スタート」ボタンを押す
以上です。
html
1 <! DOCTYPE html >
2 < html lang = " ja " >
3 < head >
4 < title > テキストの置換 </ title >
5 < meta charset = " utf-8 " >
6 </ head >
7 < body >
8 < h1 > テキストの置換 </ h1 >
9 < p >
10 置換前 < br >
11 < textarea id = " before " rows = " 8 " cols = " 40 " > </ textarea >
12 </ p >
13 < p >
14 置換したい文字列 < br >
15 < input type = " text " id = " target " value = " " >
16 </ p >
17 < p >
18 追加したい文字列 < br >
19 < input type = " text " id = " add " value = " " >
20 </ p >
21 < p >
22 何文字置きに置換する?(数字) < br >
23 < input type = " number " id = " number " value = " " >
24 </ p >
25 < p >
26 < input type = " submit " id = " submit " value = " 置換スタート " >
27 </ p >
28 < p >
29 置換後 < br >
30 < textarea id = " after " rows = " 8 " cols = " 40 " > </ textarea >
31 </ p >
32
33 < script src = " https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js " > </ script >
34 < script >
35
36 var gv ;
37 jQuery ( function ( ) { gv . replaceText ( ) ; } ) ;
38
39 ( function ( gv , $ )
40 {
41 gv . replaceText = function ( )
42 {
43 var r = new Replace (
44 {
45 before : { element : "#before" , errorMsg : "「置換前」が空だよ\n" } ,
46 target : { element : "#target" , errorMsg : "「置換したい文字列」が空だよ\n" } ,
47 add : { element : "#add" , errorMsg : "「追加したい文字列」が空だよ\n" } ,
48 number : { element : "#number" , errorMsg : [ "「何文字置きに置換する?(数字)」が空だよ" , "「何文字置きに置換する?(数字)」の数字がおかしいよ。整数で正の数のみだよ。" ] } ,
49 after : "#after"
50 } ) ;
51
52 $ ( "#submit" ) . on ( "click" , function ( ) { r . clickSubmit ( ) } ) ;
53
54
55 function Replace ( el )
56 {
57 this . el = { } ;
58 this . el . before = new Elm ( el . before ) ;
59 this . el . target = new Elm ( el . target ) ;
60 this . el . add = new Elm ( el . add ) ;
61 this . el . number = new Elm ( el . number ) ;
62 this . el . errorCheckEl = [ 'before' , 'target' , 'add' , 'number' ] ;
63 this . el . errorCheckElLength = 4 ;
64 this . el . after = $ ( el . after ) ;
65 }
66
67 Replace . prototype . clickSubmit = function ( )
68 {
69 var value = [ ] , replaceValue = { value : "" , replaceCount : 0 } ;
70 if ( this . valueCheck ( ) ) return ;
71 value = this . el . before . getVal ( ) . split ( this . el . target . getVal ( ) ) ;
72 if ( value . length <= 1 ) { alert ( "「置換したい文字列」で指定した「" + this . el . target . getVal ( ) + "」は「置換前」にないよ" ) ; return ; }
73 replaceValue = this . replace ( { resultAry : value } ) ;
74 this . showReplaceValue ( replaceValue ) ;
75 } ;
76
77 Replace . prototype . valueCheck = function ( )
78 {
79 var error = [ ] ;
80 for ( var i = 0 ; i < this . el . errorCheckElLength ; i ++ )
81 {
82 this . el [ this . el . errorCheckEl [ i ] ] . setVal ( ) ;
83 if ( this . el [ this . el . errorCheckEl [ i ] ] . isError ( ) ) error . push ( this . el [ this . el . errorCheckEl [ i ] ] . getErrorMsg ( ) ) ;
84 }
85 if ( error . length > 0 ) { alert ( error . join ( "" ) ) ; return true ; }
86 else return false ;
87 } ;
88
89 Replace . prototype . replace = function ( v )
90 {
91 var temp = { value : "" , replaceCount : 0 } ,
92 length = v . resultAry . length ;
93 for ( var i = 0 ; i < length ; i ++ )
94 {
95 temp . value += v . resultAry [ i ] ;
96 if ( i === length - 1 ) break ;
97 temp . value += this . el . target . getVal ( ) ;
98 if ( ( i + 1 ) % this . el . number . getVal ( ) === 0 )
99 {
100 temp . value += this . el . add . getVal ( ) ;
101 temp . replaceCount ++ ;
102 }
103 }
104 return temp ;
105 } ;
106
107 Replace . prototype . showReplaceValue = function ( v )
108 {
109 this . el . after . val ( v . value ) ;
110 alert ( v . replaceCount + "個の「" + this . el . target . getVal ( ) + "」を「" + this . el . target . getVal ( ) + this . el . add . getVal ( ) + "」に置換しました。" ) ;
111 } ;
112
113 function Elm ( e )
114 {
115 this . el = $ ( e . element ) ;
116 this . errorTxt = e . errorMsg ;
117 this . val = "" ;
118 this . error = null ;
119 }
120 Elm . prototype . setVal = function ( ) { this . val = this . el . val ( ) ; } ;
121 Elm . prototype . getVal = function ( ) { return this . val ; } ;
122 Elm . prototype . isError = function ( )
123 {
124 this . error = null ;
125 if ( Array . isArray ( this . errorTxt ) )
126 {
127 //only number
128 if ( this . val === "" ) this . error = this . errorTxt [ 0 ] ;
129 else if ( isNaN ( this . val ) || this . val < 0 || Math . round ( this . val ) != this . val ) this . error = this . errorTxt [ 1 ] ;
130 } else if ( this . val === "" ) this . error = this . errorTxt ;
131
132 return ( this . error != null ) ? true : false ;
133 } ;
134 Elm . prototype . getErrorMsg = function ( ) { return this . error ; } ;
135
136 } ;
137
138 } ) ( gv || ( gv = { } ) , jQuery ) ;
139 </ script >
140 </ body >
141 </ html >
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/07 14:05