「JavaScript」substring、split()とsearch()の使い方

1.substring()
var str=’abcdef’;
alert(str.substring(2, 5));
alert(str.substring(1));//bcdef1

2.split()
var str=’a*b*cd*ef’;
alert(str.split('*’));

3.search ()
var str=’acef’;
alert(str.search(‘a’));
alert(str.search(‘f’));//3
alert(str.search(‘ce’));//1
alert(str.search(‘o’));

4.match ()
var str=’asdf 34 656 cvs33′;
var re=/\d/g;
alert(str.match(re));//3,4,6,5,6,3,
var str=’asdf 34 656 cvs33′;
var re=/\d+/g;
alert(str.match(re));//34,656,33

5.replace()
var str=’asdf 34 656 cvs33′;
var re=/\d+/g;
var re2=/\d/g;
alert(str.replace(re,’*’));//asdf * * cvs*;
alert(str.replace(re2,’*’));//asdf ** *** cvs**1

JavaScript

Posted by arkgame