var reg = new RegExp('^[0-9]');
reg.test('foo');
false
reg.test('123');
true
var reg = /'^[0-9]'/;
undefined
var reg = /'^[0-9]'/;
function RegExp(){
}
undefined
var reg = /'^[0-9]'/;
reg.constructor;
function RegExp() { [native code] }
var reg = /'^[0-9]'/g;
var reg = /^\s+/;
undefined
reg
/^\s+/
var reg = new RegExp('^\s+');
undefined
var reg = new RegExp('^\s+');
undefined
reg
RegExp {}
var text = 'abc def ghi jkl';
var reg = /(\w+)\s(\w+)/;
reg.exec(text);
["abc def", "abc", "def"]
var reg = /(\w+)\s(\w+)/g;
undefined
reg
/(\w+)\s(\w+)/g
var text = 'abc def ghi jkl';
undefined
reg.exec(text);
["abc def", "abc", "def"]
reg.exec(text);
["ghi jkl", "ghi", "jkl"]
reg.exec(text);
null
var text = 'moritakenji';
var result = text.search(/^kan/g);
result;
-1
var text = 'moritakenji';
var result = text.search(/^moirta/g);
result;
-1
var text = 'moritakenji';
var result = text.search(/^(moirta)/g);
result;
-1
var text = 'abc def ghi jkl';
text.replace(/\s/,',');
"abc,def ghi jkl"
var text = 'abc def ghi jkl';
text.replace(/\s/g,',');
"abc,def,ghi,jkl"
text.replace(/(.)\s/g,',$1');
"ab,cde,fgh,ijkl"
text.replace(/(.)\s/g,function(01m,02m){return ','+ 02m});
Uncaught SyntaxError: Unexpected token ILLEGAL VM50:732
text.replace(/(.)\s/g,function(m0,m1){return ','+ m1});
"ab,cde,fgh,ijkl"
var text = 'abc def ghi jkl';
undefined
text
"abc def ghi jkl"
text.match(/\w/g);
["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"]
text.match(/(w+)\s/g);
null
text.match(/(w)\s/g);
null
text.match(/(.)\s/g);
["c ", "f ", "i "]
text.match(/(.)\s/g);
["c ", "f ", "i "]
text.match(/([a-z]{,3})\s/g);
null
text.match(/([a-z]{3})\s/g);
["abc ", "def ", "ghi "]
text.match(/([a-z]{1,2})\s/g);
["bc ", "ef ", "hi "]
text.match(/([a-z]{0,1})\s/g);
["c ", "f ", "i "]
text.match(/([a-z]{3})\s/g);
["abc ", "def ", "ghi "]
text.match(/(\w+)\s(\w+)/g);
["abc def", "ghi jkl"]
再帰呼び出し
アクセサプロパティ
with()
apply call
var gloval;
var re = /(.)@(.+).(.)/;
var callback = function(){
gloval = arguments;
return arguments[1] + 'at' + arguments[2] + 'dot' + arguments[3];
}
'kennjirmotaitata@yahoo.co.jp'.replace(re,callback);
//"kennjirmotaitataatyahoo.codotjp"
var arry = [];
var csv = 'one, two, three';
var re = csv.replace(/\s/g,'');
arry.push(re);
/<(\S+)(?:\s+.+)?>/
//配列に含めないタグ正規表現
文字列の位置。sliceとsubstringの第二引数に-1を与えた場合
var s = 'moritakenji';
s.slice(0,5);
"morit"
s.slice(0,-1)
"moritakenj"
s.substring(0,-1)
""
s.substring(1,-1)
"m"
substr(0,3)
長さ
s.charAt(0)
位置にある文字を取得
全部とるにはslice(0,-1);
検索位置を返すstring.indexOf();
'moritakenji'.indexOf('o',2)//-1
'moritakenji'.indexOf('o') //1
.コールバック関数
3つある任意の数字を2倍にして配列で返す関数を実行した後、1を足す関数を実行
//失敗
function multi(a,b){
var arr = [];
for(var i in arguments){
var result = arry.push(i *2);
}
return result;
};
var result2 = multi(3,4);
result2
//見本
var i,arr = [];
function muliti(a,b,c){
for(i = 0;i<3;i++){
arr[i] = arguments[i] *2;
}
return arr;
};
function add(a){
return a + 1;
}
var my = [];
my = muliti(10,30,40);
for(var i = 0;i<3;i++){my[i] = add(my[i])};
ループをへらすことで実行コストを抑える。
1つのコードでやる。
コールバック関数を受け取るようにして繰り返しごとに関数を呼び出す
var my = [];
function muliti(a,b,c,callback){
var i,arr = [];
for(i= 0;i<3;i++){
arr[i] = callback(arguments[i] * 2);
}
return arr;
}
function add(a){
return a + 1;
}
my = muliti(2,3,5,add);
add()の代わりに無名関数を使うとグローバル関数を減らせることができる
var my = [];
function muliti(a,b,c,callback){
var i,arr =[];
for(i=0;i<3;i++){
arr[i] = callback(arguments[i] *2);
}
return arr;
}
my = muliti(2,3,5,function(a){return a + 1})