1
I was transcribing some functions I have in PHP for javascript.
How could I make this code more efficient ?
String.prototype.substrCount = function(str){
if(str == undefined){
return 0;
}
var r = new RegExp(str, 'g');
return (this.match(r) || []).length;
}
String.prototype.mask = function(mask){
var value = onlyNumber(this); // REMOVE TUDO QUE NAO SEJA NUMERO
for(var i = 0; i <= mask.substrCount('#'); i++){
if(typeof(value[i]) === 'undefined'){
break;
}
var k = mask.indexOf('#'); // CAPTURA O PROXIMO INDICE DE #
mask = mask.split('');
mask[k] = value[i];
mask = mask.join('');
}
return mask;
}
In PHP I have it just like this :
function maskNumber($mask,$str){
$str = onlyNumber($str); // REMOVE TUDO QUE NAO SEJA NUMERO
$count = mb_substr_count($mask, '#');
for($i = 0; $i < $count; $i++){
$mask[strpos($mask,"#")] = $str[$i];
}
return $mask;
}
How does this function work ? Replaces
#
by numbers, that’s it ?– DontVoteMeDown
Yes, basically '72081338831'. Mask('###.##.###-##') = 720.813.388-31
– Guilherme Lautert
See if those answers helping.
– rray
@rray was this question I’ve been looking for for a while.
– Guilherme Lautert
That question not the opposite of what you want? now that I noticed LOL
– rray
Yes, but a while ago I was wanting to mount the PHP and did not find, so I researched more and found this, but now I want to transcribe to JS.
– Guilherme Lautert