Javascript: Remove Hyphens -


so have been working on function can't figure out how remove remainder hyphens @ end of string.

function solution(s) {   var l = s.length,   max = l - (l % 3 ? ((l + 1) % 3 ? 4 : 2) : 0);   var result = "";   for(var = 0; < max; i+=3) {     result += s.replace(/[^0-9]/gi, '').slice(i, + 3) + "-";   }   for(var = max; < l; i+=2) {     result += s.replace(/[^0-9]/gi, '').slice(i, + 2) + "-";  }    return result.slice(0,-1);  }    console.log(solution("0 - 22 1985--324"));   console.log(solution("555372654")); 

i know "result.slice(0,-1)" should've fixed issue removes 1 hyphen @ end.

return /.*[^-]/.exec(result)[0]        

Comments

Popular posts from this blog

Add new key value to json node in java -

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

javascript - Highcharts Synchronized charts with missing data points -