JavaScript 文字列操作 新しいページはコチラ

提供: yonewiki
移動: 案内, 検索
(文字列検索(表示コンテンツ内検索) find)
(文字列中の文字による分割(返却値は文字列配列) split)
 
591行: 591行:
  
 
     if (ua.indexOf("msie") >= 0 || ua.indexOf('trident/7') >= 0){
 
     if (ua.indexOf("msie") >= 0 || ua.indexOf('trident/7') >= 0){
         if (ver.indexOf("msie 6.") > 0){
+
         if (ver.indexOf("msie 6.") >= 0){
 
             name = 'msie6';
 
             name = 'msie6';
         }else if (ver.indexOf("msie 7.") >= 0){
+
         }
 +
        else if (ver.indexOf("msie 7.") >= 0){
 
             name = 'msie7';
 
             name = 'msie7';
         }else if (ver.indexOf("msie 8.") >= 0){
+
         }
 +
        else if (ver.indexOf("msie 8.") >= 0){
 
             name = 'msie8';
 
             name = 'msie8';
         }else if (ver.indexOf("msie 9.") >= 0){
+
         }
 +
        else if (ver.indexOf("msie 9.") >= 0){
 
             name = 'msie9';
 
             name = 'msie9';
         }else if (ver.indexOf("msie 10.") >= 0){
+
         }
 +
        else if (ver.indexOf("msie 10.") >= 0){
 
             name = 'msie10';
 
             name = 'msie10';
         }else if(ua.indexOf('trident/7') >= 0){
+
         }
 +
        else if(ua.indexOf('trident/7') >= 0){
 
             name = 'msie11';
 
             name = 'msie11';
 
         }else{
 
         }else{
 
             name = 'ie6 以前 or ie11より後のversion';
 
             name = 'ie6 以前 or ie11より後のversion';
 
         }
 
         }
     }else if (ua.indexOf('chrome') >= 0){
+
     }
 +
    else if (ua.indexOf('chrome') >= 0){
 
         name = 'chrome';
 
         name = 'chrome';
     }else if (ua.indexOf('safari') >= 0){
+
     }
 +
    else if (ua.indexOf('safari') >= 0){
 
         name = 'safari';
 
         name = 'safari';
     }else if (ua.indexOf('opera') >= 0){
+
     }
 +
    else if (ua.indexOf('opera') >= 0){
 
         name = 'opera';
 
         name = 'opera';
     }else if (ua.indexOf('firefox') >= 0){
+
     }
 +
    else if (ua.indexOf('firefox') >= 0){
 
         name = 'firefox';
 
         name = 'firefox';
 
     }
 
     }
635行: 644行:
 
</HTML>
 
</HTML>
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
 +
実質、69行目だけがfind関数を使っているところです。
  
  
641行: 653行:
 
[[Media:JavaScript String 02 find.html|サンプル実行結果]]
 
[[Media:JavaScript String 02 find.html|サンプル実行結果]]
  
== '''文字列検索(文字列変数先頭検索) IndexOf ''' ==
+
== '''文字列検索(文字列変数先頭検索) indexOf ''' ==
 +
*nPosition = strData17.indexOf("麻薬",0)
 +
 
 +
 
 +
上記のようにして利用します。
 +
 
 +
 
 +
:*第一引数:検索文字列
 +
:*第二引数:検索開始位置 0なら先頭。※省略も可能(省略時は0が適用)
 +
 
 +
 
 +
サンプルスクリプト
 +
<syntaxhighlight lang="javascript" line start="1">
 +
strData17 = new String("文字列検索(indexOf)");
 +
 
 +
document.write(".indexOf() : ",strData17, " ,検索位置 = ", strData17.indexOf("index",0), "</BR>");
 +
</syntaxhighlight>
 +
 
 +
 
 +
表示結果:
 +
 
 +
<yjavascript>
 +
strData17 = new String("文字列検索(indexOf)");
  
 +
document.write(".indexOf() : ",strData17, " ,検索位置 = ", strData17.indexOf("index",0), "</BR>");
 +
</yjavascript>
  
 
== '''文字列検索(文字列変数後方検索) lastIndexOf ''' ==
 
== '''文字列検索(文字列変数後方検索) lastIndexOf ''' ==
 +
*nPosition = strData17.lastIndexOf("麻薬", strData18.length - 1)
  
 +
 +
上記のようにして利用します。
 +
 +
 +
:*第一引数:検索文字列
 +
:*第二引数:検索開始位置 0なら先頭。※省略も可能(省略時は最後尾が適用, xxx.length - 1と同じことです)
 +
 +
 +
サンプルスクリプト
 +
<syntaxhighlight lang="javascript" line start="1">
 +
strData18 = new String("文字列検索(lastIndexOf)-last");
 +
 +
document.write(".indexOf() : ",strData18, " ,検索位置 = ", strData18.lastIndexOf("last", strData18.length - 1), "</BR>");
 +
</syntaxhighlight>
 +
 +
 +
表示結果:
 +
 +
<yjavascript>
 +
strData18 = new String("文字列検索(lastIndexOf)-last");
 +
 +
document.write(".lastIndexOf() : ",strData18, " ,検索位置 = ", strData18.lastIndexOf("last"), "</BR>");
 +
</yjavascript>
  
 
== '''文字列正規表現検索(返却値が一致文字列) match ''' ==
 
== '''文字列正規表現検索(返却値が一致文字列) match ''' ==
 +
正規表現とは?[https://ja.wikipedia.org/wiki/%E6%AD%A3%E8%A6%8F%E8%A1%A8%E7%8F%BE 本家Wikipediaの正規表現の記事]に詳細な解説は譲りたいですが、要するところに高度な検索手法と思えばよいと思います。更にWikipediaの解説をかいつまむと、数学者によって開発されたパターマッチモデルをテキスト検索向けとしてトンプソンという人が検索機能を開発して、UNIXのエディタや特定のプログラミング言語にとりこまれ、JAVA Scriptにも適用され、JAVA Scriptを標準化するECMA Scriptにおいても、正規表現検索が整備されたという流れになっています。パターンマッチングの解説については、[[正規表現 JAVA Script|正規表現 JAVA Script用の記事]]に記述します。
 +
 +
*strResult = strData19.match(/regular expression/)
 +
 +
 +
上記のようにして利用します。
 +
 +
 +
:*第一引数:正規表現パターンマッチ検索文字列
 +
 +
 +
サンプルスクリプト
 +
<syntaxhighlight lang="javascript" line start="1">
 +
strData19 = new String("正規表現文字列検索(regular expression)");
 +
 +
document.write(".match() : ",strData19, " ,検索結果 = ", strData19.match(/regular expression/), "</BR>");
 +
</syntaxhighlight>
 +
 +
 +
表示結果:
 +
 +
<yjavascript>
 +
strData19 = new String("正規表現文字列検索(regular expression)");
  
 +
document.write(".match() : ",strData19, " ,検索結果 = ", strData19.match(/regular expression/), "</BR>");
 +
</yjavascript>
  
 
== '''文字列正規表現検索(返却値が文字位置) search ''' ==
 
== '''文字列正規表現検索(返却値が文字位置) search ''' ==
 +
先の項目では、正規表現検索で一致する文字があるか探索し、一致した場合には、その一致した文字列を返却するメソッドとなっていましたが、このsearchメソッドでは、一致した文字の位置を数値で返却するメソッドになっています。パターンマッチングの解説については、[[正規表現 JAVA Script|正規表現 JAVA Script用の記事]]を参照して下さい。
  
 +
 +
*nPosition = strData20.search(/regular expression/)
 +
 +
 +
上記のようにして利用します。
 +
 +
 +
:*第一引数:正規表現パターンマッチ検索文字列
 +
 +
 +
サンプルスクリプト
 +
<syntaxhighlight lang="javascript" line start="1">
 +
strData20 = new String("正規表現文字列検索(regular expression)");
 +
 +
document.write(".search() : ",strData20, " ,検索結果 = ", strData20.search(/regular expression/), "</BR>");
 +
</syntaxhighlight>
 +
 +
 +
表示結果:
 +
 +
<yjavascript>
 +
strData20 = new String("正規表現文字列検索(regular expression)");
 +
 +
document.write(".search() : ",strData20, " ,検索結果 = ", strData20.search(/regular expression/), "</BR>");
 +
</yjavascript>
  
 
== '''文字列正規表現置換(返却値は置換後文字列) replace ''' ==
 
== '''文字列正規表現置換(返却値は置換後文字列) replace ''' ==
 +
正規表現検索で一致する文字があるか探索し、一致した場合には、その一致した文字列を変換文字列で置換した文字列を返却するメソッドとなっています、パターンマッチングの解説については、[[正規表現 JAVA Script|正規表現 JAVA Script用の記事]]を参照して下さい。
  
 +
 +
*strData = strData21.replace(/regular expression/,"Regular Expression")
 +
 +
 +
上記のようにして利用します。
 +
 +
 +
:*第一引数:正規表現パターンマッチ検索文字列
 +
:*第二引数:置換文字列
 +
 +
 +
サンプルスクリプト
 +
<syntaxhighlight lang="javascript" line start="1">
 +
strData21 = new String("正規表現文字列検索(regular expression)");
 +
 +
document.write(".replace() : ",strData21, " ,検索結果 = ", strData21.replace(/regular expression/,"Regular Expression"), "</BR>");
 +
</syntaxhighlight>
 +
 +
 +
表示結果:
 +
 +
<yjavascript>
 +
strData21 = new String("正規表現文字列検索(regular expression)");
 +
 +
document.write(".replace() : ",strData21, " ,検索結果 = ", strData21.replace(/regular expression/,"Regular Expression"), "</BR>");
 +
</yjavascript>
  
 
== '''文字列位置による抽出(返却値は抽出文字) charAt ''' ==
 
== '''文字列位置による抽出(返却値は抽出文字) charAt ''' ==
 +
文字列オブジェクトに対して、charAtメソッドの引数に取り出したい文字のポジションを指定すると、その一文字が取得できます。
 +
 +
*strData = strData22.charAt(1)
 +
 +
 +
:*第一引数:取り出したい文字の位置番号
 +
 +
 +
サンプルスクリプト
 +
<syntaxhighlight lang="javascript" line start="1">
 +
strData22 = new String("文字列位置による抽出(charAt)");
 +
 +
document.write(".charAt() : ",strData22, " ,検索結果 = ", strData22.charAt(2), "</BR>");
 +
</syntaxhighlight>
 +
 +
 +
表示結果:
 +
 +
<yjavascript>
 +
strData22 = new String("文字列位置による抽出(charAt)");
  
 +
document.write(".charAt() : ",strData22, " ,検索結果 = ", strData22.charAt(2), "</BR>");
 +
</yjavascript>
  
 
== '''文字列位置区間(後方位置指定可能)による抽出(返却値は抽出文字列) slice ''' ==
 
== '''文字列位置区間(後方位置指定可能)による抽出(返却値は抽出文字列) slice ''' ==
 +
文字列オブジェクトに対して、sliceメソッドの引数に取り出したい文字の開始終了ポジションを指定すると、その抽出文字列が取得できます。
  
 +
*strData = strData23.slice(8,10)
 +
 +
 +
:*第一引数:取り出したい文字の開始位置番号
 +
:*第二引数:取り出したい文字の終了位置番号、指定した文字位置は含まれないので、実際には抜き出し終了文字の次の文字位置を指定します。不数を指定すると末尾からの文字位置番号を指定できます。
 +
 +
 +
サンプルスクリプト
 +
<syntaxhighlight lang="javascript" line start="1">
 +
strData23 = new String("文字列位置による抽出(slice)");
 +
 +
document.write(".slice() : ",strData23, " ,検索結果 = ", strData23.slice(8,10), "</BR>");
 +
document.write(".slice() : ",strData23, " ,検索結果 = ", strData23.slice(8,-1), "</BR>");
 +
</syntaxhighlight>
 +
 +
 +
表示結果:
 +
 +
<yjavascript>
 +
strData23 = new String("文字列位置による抽出(slice)");
 +
 +
document.write(".slice() : ",strData23, " ,検索結果 = ", strData23.slice(8,10), "</BR>");
 +
document.write(".slice() : ",strData23, " ,検索結果 = ", strData23.slice(8,-1), "</BR>");
 +
</yjavascript>
  
 
== '''文字列位置区間による抽出(返却値は抽出文字列) substring ''' ==
 
== '''文字列位置区間による抽出(返却値は抽出文字列) substring ''' ==
 +
文字列オブジェクトに対して、substringメソッドの引数に取り出したい文字の開始終了ポジションを指定すると、その抽出文字列が取得できます。
  
 +
*strData = strData23.substring(8,10)
 +
 +
 +
:*第一引数:取り出したい文字の開始位置番号
 +
:*第二引数:取り出したい文字の終了位置番号、指定した文字位置は含まれないので、実際には抜き出し終了文字の次の文字位置を指定します。
 +
 +
 +
サンプルスクリプト
 +
<syntaxhighlight lang="javascript" line start="1">
 +
strData24 = new String("文字列位置による抽出(substring)");
 +
 +
document.write(".substring() : ",strData24, " ,検索結果 = ", strData24.substring(8,10), "</BR>");
 +
</syntaxhighlight>
 +
 +
 +
表示結果:
 +
 +
<yjavascript>
 +
strData24 = new String("文字列位置による抽出(substring)");
 +
 +
document.write(".substring() : ",strData24, " ,検索結果 = ", strData24.substring(8,10), "</BR>");
 +
</yjavascript>
  
 
== '''文字列位置と文字数による抽出(返却値は抽出文字列) substr ''' ==
 
== '''文字列位置と文字数による抽出(返却値は抽出文字列) substr ''' ==
 +
文字列オブジェクトに対して、subメソッドの引数に取り出したい文字の開始ポジションとその文字数を指定すると、その抽出文字列が取得できます。
  
 +
*strData = strData25.substr(3,7)
 +
 +
 +
:*第一引数:取り出したい文字の開始位置番号
 +
:*第二引数:取り出したい文字数。
 +
 +
 +
サンプルスクリプト
 +
<syntaxhighlight lang="javascript" line start="1">
 +
strData25 = new String("文字列位置による抽出(substr)");
 +
 +
document.write(".substring() : ",strData25, " ,検索結果 = ", strData25.substr(3,7), "</BR>");
 +
</syntaxhighlight>
 +
 +
 +
表示結果:
 +
 +
<yjavascript>
 +
strData25 = new String("文字列位置による抽出(substr)");
 +
 +
document.write(".substring() : ",strData25, " ,検索結果 = ", strData25.substr(3,7), "</BR>");
 +
</yjavascript>
  
 
== '''文字列中の文字による分割(返却値は文字列配列) split ''' ==
 
== '''文字列中の文字による分割(返却値は文字列配列) split ''' ==
 +
文字列オブジェクトに対して、splitメソッドの引数に分割したい文字Token(トークン:つまりは分割の記号とか文字列)を指定すると、その分割文字で分割した文字列が配列の要素となって取得できます。
 +
 +
*strData = strData26.split("-_-",3)
 +
 +
 +
:*第一引数:分割のトークンにしたい文字列
 +
:*第二引数:取り出したい配列数(上限)。
 +
 +
 +
サンプルスクリプト
 +
<syntaxhighlight lang="javascript" line start="1">
 +
strData26 = new String("文字列位置による抽出(split)-_-文字列1-_-文字列2-_-文字列3");
 +
 +
document.write(".split() : ",strData26, " ,検索結果 = ", strData26.split("-_-",3), "</BR>");
 +
</syntaxhighlight>
 +
 +
 +
表示結果:
 +
 +
<yjavascript>
 +
strData26 = new String("文字列位置による抽出(split)-_-文字列1-_-文字列2-_-文字列3");
 +
 +
document.write(".split() : ",strData26, " ,検索結果 = ", strData26.split("-_-",3), "</BR>");
 +
</yjavascript>
 +
 
[[JAVA Script#リファレンス]]に戻る。
 
[[JAVA Script#リファレンス]]に戻る。

2016年7月28日 (木) 00:00時点における最新版



個人用ツール
名前空間

変種
操作
案内
ツールボックス