• リファレンス類
  • ASCII文字にだけヒットさせる正規表現
    function string_isascii( string ) {
            //Ref: http://www.support-you.com/wiki/ajax/Ajax%2F999)Javascript%2F500)%A5%A8%A5%B9%A5%B1%A1%BC%A5%D7.html
            //Ref: http://www.unicode.org/charts/PDF/U0000.pdf p2
            //アルファベット以外の文字にマッチしなかったときはアルファベット
            return !string.match(/[^ -~\s–,'"’]/m)
    }
    alert('testあいうえお\n\\_ -"test'.replace(/[^ -~]|\\/mg, "_"))
    
    • [^ -~]がミソで、スペースからチルダまでにマッチしない文字ということ。
    • http://www.unicode.org/charts/PDF/U0000.pdf の2pにあるとおり、この正規表現で大方の英数記号がフォローできる。
  • 大きな配列を処理する
    • 数千の要素がある配列を複雑に処理するとき、しばらくブラウザが固まったようになる。
    • それを防いで、一応ユーザーの操作を受け付けるようにするには、一定個数ごとにsetTimeoutして非同期に実行する。
      function array_async_each(obj, func, index) {
              index = index || 0;
              var limit = 10
              var max = index + limit
              //数値だったらその数だけループ。引数はインデックス
              if( "number" == typeof obj )
              {
                      if( obj < max )
                              max = obj
                      for( ; index < max; ++index )
                              func( index )
                      if( index < obj )
                              setTimeout( function(){array_async_each(obj, func, index)} )
              }
              //文字列なら文字ごとにループ
              else if( "string" == typeof obj )
              {
                      if( obj.length < max )
                              max = obj.length
                      for( ;index < max; ++index )
                              func( obj.substr( index, 1 ) )
                      if( index < obj.length )
                              setTimeout( function(){array_async_each(obj, func, index)} )
              }
              //配列
              else if( obj && null != obj.length )
              {
                      if( obj.length < max )
                              max = obj.length
                      //配列の要素ごとにループ
                      for( ;index < max; ++index )
                              result = func( obj[index] )
                      if( index < obj.length )
                              setTimeout( function(){array_async_each(obj, func, index)} )
              }
      }
      array_async_each([1,2,3,.....], function( item ){ /*処理*/ })
      
  • GreasemonkeyTips