ついでに「?」が何個あるかも出す。
sample.js
$(function(){
if('http://google.com'.match(/[¥?]/g)){
console.log('http://google.com'.match(/[¥?]/g).length);
}else{
console.log(0);
};
});
matchで対象文字列に検索対象があるかどうかを判定。
あれば配列として、なければnullが返される。
で、nullならば0を返すようにしたものが上記。
さすがに冗長なコードなのでそれっぽくまとめると
refactoring.js
$(function(){
var hits = 'http://google.com'.match(/[¥?]/g);
var result = hits ? hits.length : 0;
console.log(result);
});
こんな感じで着地(謎)