<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>You Look Too Cool &#187; JavaScript</title>
	<atom:link href="http://stabucky.com/wp/archives/tag/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://stabucky.com/wp</link>
	<description>ゆるくつくる - stabuckyのブログ。</description>
	<lastBuildDate>Thu, 17 May 2012 12:09:09 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>PHPの配列関数のまとめ</title>
		<link>http://stabucky.com/wp/archives/3790</link>
		<comments>http://stabucky.com/wp/archives/3790#comments</comments>
		<pubDate>Fri, 20 Jan 2012 21:30:43 +0000</pubDate>
		<dc:creator>stabucky</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[配列]]></category>

		<guid isPermaLink="false">http://stabucky.com/wp/?p=3790</guid>
		<description><![CDATA[以前、JavaScriptの配列に関するメソッドについてまとめましたが、同じようにPHPの配列関数についてまとめてみました。 PHPの配列関数はたくさんあるので、とりあえず基本だけです。 関数 返り値 配列への影響 対応するJavaScriptのメソッド array_shift($a) 最初の要素を返す。 最初の要素が削除される。 a.shift() array_unshift($a,要素) 挿入後の配列の長さを返す。 先頭に要素が挿入される。 a.unshift(要素) array_pop($a) 最後の要素を返す。 最後の要素が削除される。 a.pop() array_push($a,要素) 挿入後の配列の長さを返す。 最後に要素が挿入される。 a.push(要素) array_slice($a,$i,$j) 0から数えてi番目からj個の要素を配列として返す。 なし。 a.slice(i,j) array_splice($a,$i,$j,$b); なし。 0から数えてi番目からj個の要素を削除し別の配列に置き換えた配列になる。 a.splice(i,j) array_splice($a,count($a),0,$b) なし。 配列の後に別の配列を追加した配列になる。 a.concat(配列)]]></description>
		<wfw:commentRss>http://stabucky.com/wp/archives/3790/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScriptでゼロ埋め</title>
		<link>http://stabucky.com/wp/archives/3608</link>
		<comments>http://stabucky.com/wp/archives/3608#comments</comments>
		<pubDate>Thu, 22 Dec 2011 11:56:38 +0000</pubDate>
		<dc:creator>stabucky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ゼロ埋め]]></category>

		<guid isPermaLink="false">http://stabucky.com/wp/?p=3608</guid>
		<description><![CDATA[1桁の数「5」に「0」を追加して2桁「05」で表示するような手法を「ゼロ埋め」や「パディング」などと言います。 Excelには「TEXT」、VBAには「Format」、PHPには「sprintf」という関数がありますが、JavaScriptには見当たりません。 これを作ってみます。 2桁限定 2桁で表示する場合は、正規表現で1桁の数のときだけを捉えて「0」を付加する、という処理をします。 正規表現で1桁の数は「/^$d$/」(または「/^[0-9]$/」)と表わします。 これに合致するときは「0$&#038;」に置換します。「$&#038;」は合致した全体を表します。 「(&#8220;&#8221;+n)」は数値nを文字列に変換しているところです。 nが文字列ならば関数にするまでもなく「n=n.replace(/^[0-9]$/,&#8221;0$&#038;&#8221;));」でもよいです。 function zero2&#40;n&#41;&#123; &#160; &#160; return&#40;&#40;&#34;&#34;+n&#41;.replace&#40;/^[0-9]$/,&#34;0$&#38;&#34;&#41;&#41;; &#125; 一般 一般には元の数の桁数と揃えたい桁数の差の分だけ「0」を付加するという処理になるでしょう。 桁を求めるには対数を使い「Math.floor(Math.log(n)/Math.log(10))+1」とします。ただしn=0のときエラーになりますので場合分けをします(そもそも負の数のときは正しく処理されませんが、その辺は無視しています)。 桁数dとの差を求め、その回数だけ「0」を付加します。 function zero&#40;n,d&#41;&#123; &#160; &#160; var z=&#34;&#34;; &#160; &#160; var nd=&#40;n==0&#41;?1:Math.floor&#40;Math.log&#40;n&#41;/Math.log&#40;10&#41;&#41;+1; &#160; &#160; for&#40;var i=0;i&#60;d-nd;i++&#41;z+=&#34;0&#34;; &#160; &#160; return&#40;z+n&#41;; &#125; また桁数ではなく文字列の長さで処理することもできます。こちらの方が分かりやすいかもしれません。 function zero&#40;n,d&#41;&#123; &#160; &#160; m=&#34;&#34;+n; &#160; &#160; while&#40;m.length&#60;d&#41;m=&#34;0&#34;+m; &#160; &#160; return&#40;m&#41;; &#125;]]></description>
		<wfw:commentRss>http://stabucky.com/wp/archives/3608/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScriptで配列をコピーする</title>
		<link>http://stabucky.com/wp/archives/3135</link>
		<comments>http://stabucky.com/wp/archives/3135#comments</comments>
		<pubDate>Tue, 16 Aug 2011 04:06:36 +0000</pubDate>
		<dc:creator>stabucky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[配列]]></category>

		<guid isPermaLink="false">http://stabucky.com/wp/?p=3135</guid>
		<description><![CDATA[JavaScriptで配列をコピーするときは次のような注意が必要です。 a=&#91;10,20,30&#93;; b=a; 配列変数aを配列変数bにコピーしています。 これで配列変数bをaの代わりに使うことができます。 次にpopを使ってaの最後の要素をクリアします。 a.pop&#40;&#41;; この場合、bには何も影響しないように思われますが、実行結果は次のようになります。 a=[10,20] b=[10,20] bもaと同じく最後の要素がクリアされます。 つまり「b=a」とすると各要素がコピーされるのではなく「bはaを参照する」ということになるのです。 したがって各要素をコピーするためには次のようにします。 a=&#91;10,20,30&#93;; for&#40;i=0;i&#60;a.length;i++&#41;&#123; &#160; &#160; b&#91;i&#93;=a&#91;i&#93;; &#125; a.pop&#40;&#41;; 実行結果は次の通り。 a=[10,20] b=[10,20,30] 意図通り、aだけが処理されました。 なお次のような簡単な方法もあります。 sliceを使うと配列を部分的に抜き出すことができます。 第1引数にセットした位置から第2引数にセットした位置の前まで抜き出します。 第1引数に「0」をセットし、第2引数を省略すると、最初から最後までを抜き出しますので、配列の各要素をコピーするのと同じ効果があります。 a=&#91;10,20,30&#93;; b=a.slice&#40;0&#41;; a.pop&#40;&#41;;]]></description>
		<wfw:commentRss>http://stabucky.com/wp/archives/3135/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>switchとifで速いのはどっち？(JavaScript)</title>
		<link>http://stabucky.com/wp/archives/2927</link>
		<comments>http://stabucky.com/wp/archives/2927#comments</comments>
		<pubDate>Thu, 16 Jun 2011 08:29:48 +0000</pubDate>
		<dc:creator>stabucky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[処理速度]]></category>

		<guid isPermaLink="false">http://stabucky.com/wp/?p=2927</guid>
		<description><![CDATA[前に、VBAで「SelectCaseとIfで速いのはどっち？」というのを調べてみましたが、今度はJavaScriptで同じことをしてみました。なお、SelectCaseはswitchに置き換えます。 サンプルコードは省略しますが、同様の関数を作り、10000回繰り返したときの実行時間を測りました。 サンプル 時間 配列変数 0.5124秒 switch 0.0688秒 if(単純) 0.2594秒 if(終了) 0.0359秒 if(場合分け) 0.0359秒 面白いのはifの場合分けを使った場合の効果に違いが見られなかったことです。]]></description>
		<wfw:commentRss>http://stabucky.com/wp/archives/2927/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>東日本大地震からの経過年月日を表示する</title>
		<link>http://stabucky.com/wp/archives/2922</link>
		<comments>http://stabucky.com/wp/archives/2922#comments</comments>
		<pubDate>Sat, 11 Jun 2011 14:00:37 +0000</pubDate>
		<dc:creator>stabucky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[地震]]></category>
		<category><![CDATA[日付]]></category>

		<guid isPermaLink="false">http://stabucky.com/wp/?p=2922</guid>
		<description><![CDATA[東日本大地震が起きたのが3月11日。 ちょうど3か月が経過しました。 これを自動的にカウントするJavaScriptのコードを書いてみました。 document.write&#40;count_ymd&#40;&#41;&#41;; function count_ymd&#40;&#41; &#123; &#160; &#160; var fdy = 2011, &#160; &#160; &#160; &#160; fdm = 3 - 1, &#160; &#160; &#160; &#160; fdd = 11; &#160; &#160; var td = new Date&#40;&#41;; &#160; &#160; var tdy = td.getFullYear&#40;&#41;, &#160; &#160; &#160; &#160; tdm = td.getMonth&#40;&#41;, &#160; &#160; &#160; &#160; tdd = [...]]]></description>
		<wfw:commentRss>http://stabucky.com/wp/archives/2922/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScriptの日付オブジェクトが認識する形式</title>
		<link>http://stabucky.com/wp/archives/2880</link>
		<comments>http://stabucky.com/wp/archives/2880#comments</comments>
		<pubDate>Tue, 24 May 2011 13:09:29 +0000</pubDate>
		<dc:creator>stabucky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[日付]]></category>

		<guid isPermaLink="false">http://stabucky.com/wp/?p=2880</guid>
		<description><![CDATA[JavaScriptの日付オブジェクトは、年、月、日とバラバラにセットして使いますが、日付を表す文字列をセットしても日付として認識します。 document.write&#40;new Date&#40;&#34;15 June 2008&#34;&#41;&#41;; これを実行すると次の通り表示されます。 Sun Jun 15 00:00:00 UTC+0900 2008 では、どのような日付文字列ならば正しく処理されるのでしょうか。 15 June 2008 これが標準的な英語表記ですが、この場合、(上の通り)意図通り処理されます。 15 Jun 2008 月を3文字に省略した場合も意図通り処理されます。 2008 Jun 15 年、月、日という日本式の順序でも意図通り処理されます。 2008 jun 15 月を小文字にしてみました。これもOK。 jun 15 2008 月、日、年という順序でもOK。 jun 2008 15 月、年、日と言う順序でもOK。 jun, 2008, 15 カンマ区切りでもOK。 つまり次の条件ならば意図通り処理されるようです。 年は4桁 月は3文字以上の英語表記 日は2桁以下 スペース区切りまたはカンマ区切り 一方、次のような文字列はダメでした。 2008-6-15 2008-jun-15]]></description>
		<wfw:commentRss>http://stabucky.com/wp/archives/2880/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>「同じ文字が2個並んだ文字列」の正規表現</title>
		<link>http://stabucky.com/wp/archives/2869</link>
		<comments>http://stabucky.com/wp/archives/2869#comments</comments>
		<pubDate>Mon, 23 May 2011 12:28:44 +0000</pubDate>
		<dc:creator>stabucky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[正規表現]]></category>
		<category><![CDATA[秀丸]]></category>

		<guid isPermaLink="false">http://stabucky.com/wp/?p=2869</guid>
		<description><![CDATA[「book」の「oo」のように文字の繰り返しを見付ける正規表現について調べました。 JavaScriptの場合 まず/.{2}/という表現を思い付きますが、これでは「同じ文字が2個並んだ文字列」になりません。単に「文字が2個並んだ文字列」です。1個目と2個目の文字が異なってもよいからです。 そこで次のようにします。 str=&#34;book&#34;; ret=str.match&#40;/(.)\1/g&#41;; 「oo」が返されます(正確にはret[0]として)。 JavaScriptの正規表現では「\1」を使うと括弧に挟まれた部分を取得できます。 /(.)/とすると任意の1文字が取得でき、\1に格納されます。 /(.)\1/とすると/\1\1/のように「同じ文字が2個並んだ文字列」を探すことになります。 秀丸の場合 検索文字列として「(.)\1」を指定します。 もちろん正規表現にチェックを入れます。 もし「同じ文字が2個以上並んだ文字列」を検索するならば「(.)\1+」とするとよいでしょう。]]></description>
		<wfw:commentRss>http://stabucky.com/wp/archives/2869/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScriptのmatchの返り値</title>
		<link>http://stabucky.com/wp/archives/2867</link>
		<comments>http://stabucky.com/wp/archives/2867#comments</comments>
		<pubDate>Mon, 23 May 2011 12:25:05 +0000</pubDate>
		<dc:creator>stabucky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://stabucky.com/wp/?p=2867</guid>
		<description><![CDATA[JavaScriptのmatchは正規表現に合致した文字列を配列で返します。 「g」を付けない場合と「g」を付ける場合で内容が変わります。 gを付けない場合 gを付ける場合 0個目　合致した部分全体 1個目　1個目の括弧で括られた部分 2個目　2個目の括弧で括られた部分 0個目　最初に合致した部分全体 1個目　2番目に合致した部分全体 gを付けない場合のサンプル str=&#34;xayxbyxcy&#34;; ret=str.match&#40;/(x.)y/&#41;; ret[0]=&#8221;xay&#8221; ret[1]=&#8221;xa&#8221; 「ｘ□ｙ」という文字列を探しています。 gがないので一つ見つけたら終わりです。 返り値の0個目は合致した部分全体です。「xay」になります。 返り値の1個目は合致した部分のうち括弧に挟まれた部分です。「(xa)y」と見て「xa」になります。 gを付ける場合のサンプル str=&#34;xayxbyxcy&#34;; ret=str.match&#40;/(x.)y/g&#41;; ret[0]=&#8221;xay&#8221; ret[1]=&#8221;xby&#8221; ret[2]=&#8221;xcy&#8221; 上のサンプルと同じく「ｘ□ｙ」という文字列を探しています。 gがあるので合致する文字列をすべて見付けます。 返り値の0個目は最初に合致した文字列です。 返り値の1個目は2番目に合致した文字列です。 返り値の2個目は3番目に合致した文字列です。 括弧に挟まれた部分を返すようなことはありません。]]></description>
		<wfw:commentRss>http://stabucky.com/wp/archives/2867/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>長音を含む文字列を辞書順でソート</title>
		<link>http://stabucky.com/wp/archives/2797</link>
		<comments>http://stabucky.com/wp/archives/2797#comments</comments>
		<pubDate>Fri, 06 May 2011 20:37:56 +0000</pubDate>
		<dc:creator>stabucky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ソート]]></category>

		<guid isPermaLink="false">http://stabucky.com/wp/?p=2797</guid>
		<description><![CDATA[辞書の言葉の並び順はアイウエオ順ですが、「ー」という伸ばす音、長音があるときは、その前の音によって判断されます。 その前の音がア段ならば「ア」、イ段ならば「イ」となります。 例えば「ワード」ならば「ワアド」、「キー」ならば「キイ」として並び順が決まります。 ワア≫ワー≫ワアド≫ワード≫ワイ となります。 Excel(2007)では、辞書と同様にソートされます。 しかし、OpenOfficeのCalcでは、辞書順でなくコード順でソートされます。 また、JavaScriptのsortでも、コード順でソートされます。 つまり「ー」はコード順では「ン」より後ですから ワア≫ワイ≫ワン≫ワー のようになってしまいます。 これを辞書順でソートするためには、まず長音を「アイウエオ」に変換してからソートする必要があります。 JavaScriptのコードを書いてみました。 まずソートの部分。配列を渡すと長音を「アイウエオ」に変換して昇順でソートして返します。 function aiueo_sort&#40;words&#41; &#123; &#160; &#160; words.sort&#40;function &#40;a, b&#41; &#123; &#160; &#160; &#160; &#160; if &#40;a == b&#41; &#123; &#160; &#160; &#160; &#160; &#160; &#160; return &#40;0&#41;; &#160; &#160; &#160; &#160; &#125; &#160; &#160; &#160; &#160; var ca = choon_conv&#40;a&#41;; &#160; &#160; [...]]]></description>
		<wfw:commentRss>http://stabucky.com/wp/archives/2797/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSONの\uXXXXをデコード</title>
		<link>http://stabucky.com/wp/archives/2650</link>
		<comments>http://stabucky.com/wp/archives/2650#comments</comments>
		<pubDate>Sat, 16 Apr 2011 09:05:42 +0000</pubDate>
		<dc:creator>stabucky</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JSON]]></category>

		<guid isPermaLink="false">http://stabucky.com/wp/?p=2650</guid>
		<description><![CDATA[TwitterAPIなどを使い、JSON形式でデータを取得した場合、文字列が次のようになっている場合があります。 \u4eca\u671d\u306f 初めて見ると「文字化けか」と思ってしまうかもしれませんが、これはJSONのルール通りなんだそうです。 この「\uXXXX」形式は「Unicode文字エスケープシーケンス」、「unicode escape sequence」などと呼ぶそうです。 2.4.1 Unicode 文字エスケープ シーケンス (C#) Unicodeにおける文字番号を4桁の16進数に置き換えて、頭に「\u」を付けます。 これをJavaScriptを使って人間が読める形にする方法(デコード)を調べました。 単純な方法 簡単なのは次のようにJavaScriptでページに書く方法です。 document.write&#40;&#34;\u4eca\u671d\u306f&#34;&#41;; これで次のように表示されます。 今朝は 関数を使う方法 「json_str_decode」という関数を作ってみました。 このケースでは「\」が(JavaScriptの)エスケープシーケンスに該当してしまうので「\\」のようにしないと正しく認識しません。 document.write&#40;json_str_decode&#40;&#34;\\u4eca\\u671d\\u306f&#34;&#41;&#41;; function json_str_decode&#40;str&#41;&#123; &#160; &#160; arrs=str.match&#40;/\\u.{4}/g&#41;; &#160; &#160; var t=&#34;&#34;; &#160; &#160; for&#40;i=0;i&#60;arrs.length;i++&#41;&#123; &#160; &#160; &#160; &#160; t+=String.fromCharCode&#40;arrs&#91;i&#93;.replace&#40;&#34;\\u&#34;,&#34;0x&#34;&#41;&#41;; &#160; &#160; &#125; &#160; &#160; return&#40;t&#41;; &#125; 次のようにテキストエリアなどに貼り付けて使う場合には「\\」とする必要がありません。 &#60;textarea id=&#34;ta&#34;&#62;\u4eca\u671d\u306f&#60;/textarea&#62; &#60;div id=&#34;mydiv&#34;&#62;&#60;/div&#62; var a=document.getElementById&#40;&#34;ta&#34;&#41;.value; document.write&#40;json_str_decode&#40;a&#41;&#41;;]]></description>
		<wfw:commentRss>http://stabucky.com/wp/archives/2650/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

