複数の文字を置換する方法(JS、replace)
複数ワードの文字置換
「サイト内に掲載している特定文字(複数)を一括で変換したい」そんな時が来たらの考察。
(例えば、サイト全体に散らばっているある文字列を全部削除や書き換えたい、更には書き換える文字が複数ある場合に)
コンテンツ部分の文字置換はWordPress等のフレームワークで作成されている場合は、文字置換の良いプラグインがあるので、用途に合わせて利用されるのが良いです。
ここで記載するのは、それぞれ色々と理由があって、どうしてもJavaScriptで複数文字のリプレイスをしたいといった場合です。
JavaScriptで置換(replace)
複数ある文字を上手に管理、動かす際はJSONが便利、汎用性があると思いましたので。
例として、「平成31年を、2019年(西暦)」や、「平成32年を、令和2年」、
(和暦の元年変換は日付の判別にUNIXTIMEの計算が入ってくるので、ここでは割愛します)
json作成(sample.json)
基本UTF-8で、適所に配置。
[{ "division": "西暦和暦", "element": [ { "before": "平成112", "after": "令和82","ad": "2100"}, { "before": "平成35", "after": "令和5","ad": "2023"}, { "before": "平成34", "after": "令和4","ad": "2022"}, { "before": "平成33", "after": "令和3","ad": "2021"}, { "before": "平成32", "after": "令和2","ad": "2020"}, { "before": "平成31", "after": "平成31(令和元年)","ad": "2019"} ] }]
htmlの準備(test.html)
適当にありえない和暦、全角半角を含めたものを用意しました。
<ul> <li>平成31年</li> <li>令和元年</li> <li>平成32年</li> <li>平成32年</li> <li>平成33年</li> <li>平成112年</li> </ul>
htmlにjsを記述する(test.html)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <script language="JavaScript" type="text/javascript"> $(document).ready(function () { $.getJSON('【httpから始めるjson格納パス】/sample.json', function(data){ for(var i in data){ for(var j in data[i].element){ var txt = $('body').html(); $('body').each(function(){ $(this).html(txt.replace(new RegExp((data[i].element[j].before), 'ig') , data[i].element[j].after)); }); } } }) }); // 全角数字を探す場合(ついでに、jsonで用意したadカラムの値でリプレイス) $(document).ready(function () { $.getJSON('【httpから始めるjson格納パス】/sample.json', function(data){ for(var i in data){ for(var j in data[i].element){ var txt = $('body').html(); $('body').each(function(){ $(this).html(txt.replace(new RegExp((data[i].element[j].before.replace(/[A-Za-z0-9!-~]/g, function(s){ return String.fromCharCode(s.charCodeAt(0)+0xFEE0); })), 'ig') , data[i].element[j].ad)); }); } } }) }); </script>
まとめ
丁度この記事を作成したのが2019年4月という、新年号切り替え(5月1日)直前で、
サイトに携わっている身分として、「何か準備が必要なのでは」という脅迫観念からの内容となりました。
一つや二つの文字置換ならそう使わないですが、機械的に複数文字の削除or置換をJSで実行したい場合に役に立つこともあるかも。
最後に
ついでに、暇を拗らせUNIXTIME入りのJSONを用意しました、もし使えるタイミングが出てきたら嬉しいです。
とはいえ、令和82年とか、現在の科学的常識から考えにくいものなので、またいつかは書き換える必要が出てくるし、そもそも令和の次が何か、分からないし。数十年単位で見たらJavascriptがオワコンなってるかもしれないし。
そう思うと、この手のリプレイスは応急処置程度にしかならないですね。
当人、このUNIXTIMEでの日付計算しようとして、色々条件が発生し複雑な事を知りました。
1900年より前の和暦計算やら、それをクリアしても明治5年に太陰太陽暦から切り替わったりドツボに嵌りそうでしたので断念。
[{ "division": "西暦和暦", "element": [ ,{ "before": "令和82", "after": "2100","ad": "2100","unix": 4102412400} ,{ "before": "令和81", "after": "2099","ad": "2099","unix": 4070876400} ,{ "before": "令和80", "after": "2098","ad": "2098","unix": 4039340400} ,{ "before": "令和79", "after": "2097","ad": "2097","unix": 4007804400} ,{ "before": "令和78", "after": "2096","ad": "2096","unix": 3976182000} ,{ "before": "令和77", "after": "2095","ad": "2095","unix": 3944646000} ,{ "before": "令和76", "after": "2094","ad": "2094","unix": 3913110000} ,{ "before": "令和75", "after": "2093","ad": "2093","unix": 3881574000} ,{ "before": "令和74", "after": "2092","ad": "2092","unix": 3849951600} ,{ "before": "令和73", "after": "2091","ad": "2091","unix": 3818415600} ,{ "before": "令和72", "after": "2090","ad": "2090","unix": 3786879600} ,{ "before": "令和71", "after": "2089","ad": "2089","unix": 3755343600} ,{ "before": "令和70", "after": "2088","ad": "2088","unix": 3723721200} ,{ "before": "令和69", "after": "2087","ad": "2087","unix": 3692185200} ,{ "before": "令和68", "after": "2086","ad": "2086","unix": 3660649200} ,{ "before": "令和67", "after": "2085","ad": "2085","unix": 3629113200} ,{ "before": "令和66", "after": "2084","ad": "2084","unix": 3597490800} ,{ "before": "令和65", "after": "2083","ad": "2083","unix": 3565954800} ,{ "before": "令和64", "after": "2082","ad": "2082","unix": 3534418800} ,{ "before": "令和63", "after": "2081","ad": "2081","unix": 3502882800} ,{ "before": "令和62", "after": "2080","ad": "2080","unix": 3471260400} ,{ "before": "令和61", "after": "2079","ad": "2079","unix": 3439724400} ,{ "before": "令和60", "after": "2078","ad": "2078","unix": 3408188400} ,{ "before": "令和59", "after": "2077","ad": "2077","unix": 3376652400} ,{ "before": "令和58", "after": "2076","ad": "2076","unix": 3345030000} ,{ "before": "令和57", "after": "2075","ad": "2075","unix": 3313494000} ,{ "before": "令和56", "after": "2074","ad": "2074","unix": 3281958000} ,{ "before": "令和55", "after": "2073","ad": "2073","unix": 3250422000} ,{ "before": "令和54", "after": "2072","ad": "2072","unix": 3218799600} ,{ "before": "令和53", "after": "2071","ad": "2071","unix": 3187263600} ,{ "before": "令和52", "after": "2070","ad": "2070","unix": 3155727600} ,{ "before": "令和51", "after": "2069","ad": "2069","unix": 3124191600} ,{ "before": "令和50", "after": "2068","ad": "2068","unix": 3092569200} ,{ "before": "令和49", "after": "2067","ad": "2067","unix": 3061033200} ,{ "before": "令和48", "after": "2066","ad": "2066","unix": 3029497200} ,{ "before": "令和47", "after": "2065","ad": "2065","unix": 2997961200} ,{ "before": "令和46", "after": "2064","ad": "2064","unix": 2966338800} ,{ "before": "令和45", "after": "2063","ad": "2063","unix": 2934802800} ,{ "before": "令和44", "after": "2062","ad": "2062","unix": 2903266800} ,{ "before": "令和43", "after": "2061","ad": "2061","unix": 2871730800} ,{ "before": "令和42", "after": "2060","ad": "2060","unix": 2840108400} ,{ "before": "令和41", "after": "2059","ad": "2059","unix": 2808572400} ,{ "before": "令和40", "after": "2058","ad": "2058","unix": 2777036400} ,{ "before": "令和39", "after": "2057","ad": "2057","unix": 2745500400} ,{ "before": "令和38", "after": "2056","ad": "2056","unix": 2713878000} ,{ "before": "令和37", "after": "2055","ad": "2055","unix": 2682342000} ,{ "before": "令和36", "after": "2054","ad": "2054","unix": 2650806000} ,{ "before": "令和35", "after": "2053","ad": "2053","unix": 2619270000} ,{ "before": "令和34", "after": "2052","ad": "2052","unix": 2587647600} ,{ "before": "令和33", "after": "2051","ad": "2051","unix": 2556111600} ,{ "before": "令和32", "after": "2050","ad": "2050","unix": 2524575600} ,{ "before": "令和31", "after": "2049","ad": "2049","unix": 2493039600} ,{ "before": "令和30", "after": "2048","ad": "2048","unix": 2461417200} ,{ "before": "令和29", "after": "2047","ad": "2047","unix": 2429881200} ,{ "before": "令和28", "after": "2046","ad": "2046","unix": 2398345200} ,{ "before": "令和27", "after": "2045","ad": "2045","unix": 2366809200} ,{ "before": "令和26", "after": "2044","ad": "2044","unix": 2335186800} ,{ "before": "令和25", "after": "2043","ad": "2043","unix": 2303650800} ,{ "before": "令和24", "after": "2042","ad": "2042","unix": 2272114800} ,{ "before": "令和23", "after": "2041","ad": "2041","unix": 2240578800} ,{ "before": "令和22", "after": "2040","ad": "2040","unix": 2208956400} ,{ "before": "令和21", "after": "2039","ad": "2039","unix": 2177420400} ,{ "before": "令和20", "after": "2038","ad": "2038","unix": 2145884400} ,{ "before": "令和19", "after": "2037","ad": "2037","unix": 2114348400} ,{ "before": "令和18", "after": "2036","ad": "2036","unix": 2082726000} ,{ "before": "令和17", "after": "2035","ad": "2035","unix": 2051190000} ,{ "before": "令和16", "after": "2034","ad": "2034","unix": 2019654000} ,{ "before": "令和15", "after": "2033","ad": "2033","unix": 1988118000} ,{ "before": "令和14", "after": "2032","ad": "2032","unix": 1956495600} ,{ "before": "令和13", "after": "2031","ad": "2031","unix": 1924959600} ,{ "before": "令和12", "after": "2030","ad": "2030","unix": 1893423600} ,{ "before": "令和11", "after": "2029","ad": "2029","unix": 1861887600} ,{ "before": "令和10", "after": "2028","ad": "2028","unix": 1830265200} ,{ "before": "令和9", "after": "2027","ad": "2027","unix": 1798729200} ,{ "before": "令和8", "after": "2026","ad": "2026","unix": 1767193200} ,{ "before": "令和7", "after": "2025","ad": "2025","unix": 1735657200} ,{ "before": "令和6", "after": "2024","ad": "2024","unix": 1704034800} ,{ "before": "令和5", "after": "2023","ad": "2023","unix": 1672498800} ,{ "before": "令和4", "after": "2022","ad": "2022","unix": 1640962800} ,{ "before": "令和3", "after": "2021","ad": "2021","unix": 1609426800} ,{ "before": "令和2", "after": "2020","ad": "2020","unix": 1577804400} ,{ "before": "令和元", "after": "2019","ad": "2019","unix": 1556636400} ,{ "before": "平成31", "after": "2019","ad": "2019","unix": 1546268400} ,{ "before": "平成30", "after": "2018","ad": "2018","unix": 1514732400} ,{ "before": "平成29", "after": "2017","ad": "2017","unix": 1483196400} ,{ "before": "平成28", "after": "2016","ad": "2016","unix": 1451574000} ,{ "before": "平成27", "after": "2015","ad": "2015","unix": 1420038000} ,{ "before": "平成26", "after": "2014","ad": "2014","unix": 1388502000} ,{ "before": "平成25", "after": "2013","ad": "2013","unix": 1356966000} ,{ "before": "平成24", "after": "2012","ad": "2012","unix": 1325343600} ,{ "before": "平成23", "after": "2011","ad": "2011","unix": 1293807600} ,{ "before": "平成22", "after": "2010","ad": "2010","unix": 1262271600} ,{ "before": "平成21", "after": "2009","ad": "2009","unix": 1230735600} ,{ "before": "平成20", "after": "2008","ad": "2008","unix": 1199113200} ,{ "before": "平成19", "after": "2007","ad": "2007","unix": 1167577200} ,{ "before": "平成18", "after": "2006","ad": "2006","unix": 1136041200} ,{ "before": "平成17", "after": "2005","ad": "2005","unix": 1104505200} ,{ "before": "平成16", "after": "2004","ad": "2004","unix": 1072882800} ,{ "before": "平成15", "after": "2003","ad": "2003","unix": 1041346800} ,{ "before": "平成14", "after": "2002","ad": "2002","unix": 1009810800} ,{ "before": "平成13", "after": "2001","ad": "2001","unix": 978274800} ,{ "before": "平成12", "after": "2000","ad": "2000","unix": 946652400} ,{ "before": "平成11", "after": "1999","ad": "1999","unix": 915116400} ,{ "before": "平成10", "after": "1998","ad": "1998","unix": 883580400} ,{ "before": "平成9", "after": "1997","ad": "1997","unix": 852044400} ,{ "before": "平成8", "after": "1996","ad": "1996","unix": 820422000} ,{ "before": "平成7", "after": "1995","ad": "1995","unix": 788886000} ,{ "before": "平成6", "after": "1994","ad": "1994","unix": 757350000} ,{ "before": "平成5", "after": "1993","ad": "1993","unix": 725814000} ,{ "before": "平成4", "after": "1992","ad": "1992","unix": 694191600} ,{ "before": "平成3", "after": "1991","ad": "1991","unix": 662655600} ,{ "before": "平成2", "after": "1990","ad": "1990","unix": 631119600} ,{ "before": "平成元", "after": "1989","ad": "1989","unix": 600188400} ,{ "before": "昭和64", "after": "1989","ad": "1989","unix": 599583600} ,{ "before": "昭和63", "after": "1988","ad": "1988","unix": 567961200} ,{ "before": "昭和62", "after": "1987","ad": "1987","unix": 536425200} ,{ "before": "昭和61", "after": "1986","ad": "1986","unix": 504889200} ,{ "before": "昭和60", "after": "1985","ad": "1985","unix": 473353200} ,{ "before": "昭和59", "after": "1984","ad": "1984","unix": 441730800} ,{ "before": "昭和58", "after": "1983","ad": "1983","unix": 410194800} ,{ "before": "昭和57", "after": "1982","ad": "1982","unix": 378658800} ,{ "before": "昭和56", "after": "1981","ad": "1981","unix": 347122800} ,{ "before": "昭和55", "after": "1980","ad": "1980","unix": 315500400} ,{ "before": "昭和54", "after": "1979","ad": "1979","unix": 283964400} ,{ "before": "昭和53", "after": "1978","ad": "1978","unix": 252428400} ,{ "before": "昭和52", "after": "1977","ad": "1977","unix": 220892400} ,{ "before": "昭和51", "after": "1976","ad": "1976","unix": 189270000} ,{ "before": "昭和50", "after": "1975","ad": "1975","unix": 157734000} ,{ "before": "昭和49", "after": "1974","ad": "1974","unix": 126198000} ,{ "before": "昭和48", "after": "1973","ad": "1973","unix": 94662000} ,{ "before": "昭和47", "after": "1972","ad": "1972","unix": 63039600} ,{ "before": "昭和46", "after": "1971","ad": "1971","unix": 31503600} ,{ "before": "昭和45", "after": "1970","ad": "1970","unix": -32400} ,{ "before": "昭和44", "after": "1969","ad": "1969","unix": -31503600} ,{ "before": "昭和43", "after": "1968","ad": "1968","unix": -63126000} ,{ "before": "昭和42", "after": "1967","ad": "1967","unix": -94662000} ,{ "before": "昭和41", "after": "1966","ad": "1966","unix": -126198000} ,{ "before": "昭和40", "after": "1965","ad": "1965","unix": -157734000} ,{ "before": "昭和39", "after": "1964","ad": "1964","unix": -189356400} ,{ "before": "昭和38", "after": "1963","ad": "1963","unix": -220892400} ,{ "before": "昭和37", "after": "1962","ad": "1962","unix": -252428400} ,{ "before": "昭和36", "after": "1961","ad": "1961","unix": -283964400} ,{ "before": "昭和35", "after": "1960","ad": "1960","unix": -315586800} ,{ "before": "昭和34", "after": "1959","ad": "1959","unix": -347122800} ,{ "before": "昭和33", "after": "1958","ad": "1958","unix": -378658800} ,{ "before": "昭和32", "after": "1957","ad": "1957","unix": -410194800} ,{ "before": "昭和31", "after": "1956","ad": "1956","unix": -441817200} ,{ "before": "昭和30", "after": "1955","ad": "1955","unix": -473353200} ,{ "before": "昭和29", "after": "1954","ad": "1954","unix": -504889200} ,{ "before": "昭和28", "after": "1953","ad": "1953","unix": -536425200} ,{ "before": "昭和27", "after": "1952","ad": "1952","unix": -568047600} ,{ "before": "昭和26", "after": "1951","ad": "1951","unix": -599583600} ,{ "before": "昭和25", "after": "1950","ad": "1950","unix": -631119600} ,{ "before": "昭和24", "after": "1949","ad": "1949","unix": -662655600} ,{ "before": "昭和23", "after": "1948","ad": "1948","unix": -694278000} ,{ "before": "昭和22", "after": "1947","ad": "1947","unix": -725814000} ,{ "before": "昭和21", "after": "1946","ad": "1946","unix": -757350000} ,{ "before": "昭和20", "after": "1945","ad": "1945","unix": -788886000} ,{ "before": "昭和19", "after": "1944","ad": "1944","unix": -820508400} ,{ "before": "昭和18", "after": "1943","ad": "1943","unix": -852044400} ,{ "before": "昭和17", "after": "1942","ad": "1942","unix": -883580400} ,{ "before": "昭和16", "after": "1941","ad": "1941","unix": -915116400} ,{ "before": "昭和15", "after": "1940","ad": "1940","unix": -946738800} ,{ "before": "昭和14", "after": "1939","ad": "1939","unix": -978274800} ,{ "before": "昭和13", "after": "1938","ad": "1938","unix": -1009810800} ,{ "before": "昭和12", "after": "1937","ad": "1937","unix": -1041346800} ,{ "before": "昭和11", "after": "1936","ad": "1936","unix": -1072969200} ,{ "before": "昭和10", "after": "1935","ad": "1935","unix": -1104505200} ,{ "before": "昭和9", "after": "1934","ad": "1934","unix": -1136041200} ,{ "before": "昭和8", "after": "1933","ad": "1933","unix": -1167577200} ,{ "before": "昭和7", "after": "1932","ad": "1932","unix": -1199199600} ,{ "before": "昭和6", "after": "1931","ad": "1931","unix": -1230735600} ,{ "before": "昭和5", "after": "1930","ad": "1930","unix": -1262271600} ,{ "before": "昭和4", "after": "1929","ad": "1929","unix": -1293807600} ,{ "before": "昭和3", "after": "1928","ad": "1928","unix": -1325430000} ,{ "before": "昭和2", "after": "1927","ad": "1927","unix": -1356966000} ,{ "before": "昭和元", "after": "1926","ad": "1926","unix": -1357635600} ,{ "before": "大正15", "after": "1926","ad": "1926","unix": -1388502000} ,{ "before": "大正14", "after": "1925","ad": "1925","unix": -1420038000} ,{ "before": "大正13", "after": "1924","ad": "1924","unix": -1451660400} ,{ "before": "大正12", "after": "1923","ad": "1923","unix": -1483196400} ,{ "before": "大正11", "after": "1922","ad": "1922","unix": -1514732400} ,{ "before": "大正10", "after": "1921","ad": "1921","unix": -1546268400} ,{ "before": "大正9", "after": "1920","ad": "1920","unix": -1577890800} ,{ "before": "大正8", "after": "1919","ad": "1919","unix": -1609426800} ,{ "before": "大正7", "after": "1918","ad": "1918","unix": -1640962800} ,{ "before": "大正6", "after": "1917","ad": "1917","unix": -1672498800} ,{ "before": "大正5", "after": "1916","ad": "1916","unix": -1704121200} ,{ "before": "大正4", "after": "1915","ad": "1915","unix": -1735657200} ,{ "before": "大正3", "after": "1914","ad": "1914","unix": -1767193200} ,{ "before": "大正2", "after": "1913","ad": "1913","unix": -1798729200} ,{ "before": "大正元", "after": "1912","ad": "1912","unix": -1812186000} ,{ "before": "明治45", "after": "1912","ad": "1912","unix": -1830351600} ,{ "before": "明治44", "after": "1911","ad": "1911","unix": -1861887600} ,{ "before": "明治43", "after": "1910","ad": "1910","unix": -1893423600} ,{ "before": "明治42", "after": "1909","ad": "1909","unix": -1924959600} ,{ "before": "明治41", "after": "1908","ad": "1908","unix": -1956582000} ,{ "before": "明治40", "after": "1907","ad": "1907","unix": -1988118000} ,{ "before": "明治39", "after": "1906","ad": "1906","unix": -2019654000} ,{ "before": "明治38", "after": "1905","ad": "1905","unix": -2051190000} ,{ "before": "明治37", "after": "1904","ad": "1904","unix": -2082812400} ,{ "before": "明治36", "after": "1903","ad": "1903","unix": -2114348400} ,{ "before": "明治35", "after": "1902","ad": "1902","unix": -2145884400} ,{ "before": "明治34", "after": "1901","ad": "1901","unix": -2177420400} ,{ "before": "明治33", "after": "1900","ad": "1900","unix": -2208956400} ] }]