2017-06-12 60 views
3

所以,我有幾個測試用例字符串:JavaScript函數格式字符串

測試:00 -44 48 5555 8361' 預計:004-448-555-583-61

測試:0 - 22 1985--324' 預計:022-198-53-24

測試: '555372654' 預計:555-372-654

任何建議將是有益的。我一直在這裏待了幾個小時,試圖找出爲什麼一些預期的結果有效,而另一些則沒有。給定表示電話號碼的字符串S,字符串將需要重新格式化。字符串S由N個字符組成:數字,空格和/或破折號。它至少包含兩位數字。字符串S中的空格和破折號可以忽略。我想重新格式化給定的電話號碼,以便數字按長度爲三的塊分組,並用單個破折號分隔。如果有必要,最後一個塊或最後兩個塊的長度可以是兩個。

var s = "00-44 48 5555 8361"; 
 
var n = s.replace(/\D/g,''); 
 
var temp = formatNumber(n); 
 
var final = temp.replace(/\-$/,''); 
 
alert(final); 
 

 
function formatNumber(S){ 
 
    var len = S.length; 
 
    var max = len - 2 * ((3-len) % 3); 
 
    var dash = "-"; 
 
    var result; 
 
    for(i = 0; i < max; i+=3){ 
 
    \t result += S.substring(i, i+3).concat(dash); 
 
    } 
 
    for(i = max; i < len; i+=2){ 
 
    \t result += S.substring(i, i+2).concat(dash); 
 
    } 
 
\t return result.toString(); 
 
}

+4

*作用有點滑稽* - 不是一個問題描述 –

+0

爲什麼第二個測試案例具有二個長度爲預期結果的兩個子集? –

+1

'formatNumber'的用途是什麼? –

回答

0

在行var max = len - 2 * ((3-len) % 3);,實際上是增加了len未從其減去按預期(因爲len >= 3從而3 - len <= 0從而(3 - len) % 3 <= 0因此通過-2倍增時,它的結果將是肯定的)。所以max總是等於或大於len的長度。

試試這個:

max = len - (len % 3? ((len + 1) % 3? 4: 2): 0); 

可以通過解釋:

var max = len; 
if(len % 3 === 0) {    // if len is divisible by 3 (if len === 0, 3, 6, 9, ...) 
    max -= 0;      // substract 0 (leave max === len) 
} else {       // if len is not divisible by 3 
    if((len + 1) % 3 === 0) {  // if len + 1 is divisible by 3 (if len === 2, 5, 8, 11, ...) 
     max -= 2;     // substract 2 (only one part containing 2 digits) 
    } else {      // if len === 1, 4, 7, 10, ... 
     max -= 4;     // substract 4 (two parts containing 2 digits) 
    } 
} 

function formatNumber(s) { 
 
    var l = s.length, 
 
     max = l - (l % 3? ((l + 1) % 3? 4: 2): 0); 
 
    var result = ""; 
 
    for(var i = 0; i < max; i+=3) { 
 
    result += s.slice(i, i + 3) + "-";   // no need for concat just add "-" to the result of slice 
 
    } 
 
    for(var i = max; i < l; i+=2) { 
 
    result += s.slice(i, i + 2) + "-"; 
 
    } 
 
    return result.slice(0, -1);     // the same as result.slice(0, result.length - 1) to remove the last '-' 
 
} 
 

 
console.log(formatNumber("123456789")); 
 
console.log(formatNumber("12345678")); 
 
console.log(formatNumber("1234567890"));

0

看來,你總是需要這樣的格式:(\d{3}-)+

function formatter(input) { 
 
    return input 
 
    
 
    // rm ws 
 
    .replace(/\s+/g, '') 
 
    
 
    // rm dashes 
 
    .replace(/-/g, '') 
 
    
 
    // split by 3 chars 
 
    .split(/(.{3})/) 
 
    
 
    .filter(g => g) 
 
    
 
    .join('-') 
 
    ; 
 
} 
 

 

 
var raw = [ 
 
    "00-44 48 5555 8361", 
 
    "555372654", 
 
    "0 - 22 1985--324" 
 
]; 
 

 
console.log('Formatted', raw.map(formatter));

2

你可以嘗試:

var phones = [ 
 
    '00-44 48 5555 8361', 
 
    '0 - 22 1985--324', 
 
    '555372654' 
 
]; 
 

 
phones = phones.map(function(phone) { 
 
    var num = phone.replace(/\D/g, ''); 
 
    return num.replace(/(...?)(?!.?$)/g, '$1-'); 
 
}); 
 

 
console.log(phones);

0
var n = '00-44 48 5555 8361'; 

console.log(formatPhone(n, 3)); 

function formatPhone(num, spacing) { 
    num = (num+'').replace(/[^\d]/g, '').split(''); 

    return num.map(function (currentValue, index) { 
     // Plus 1 because it's 0-indexed. 
     return (index + 1) % spacing === 0 
       ? currentValue + '-' 
       : currentValue 
     ; 
    }).join(''); 
} 

https://jsfiddle.net/efpy3vLn/

0

你可以做任何事情,只需幾個正則表達式替換:

// Initial string 
var s0 = "00-44 48 5555 8361"; 

// Remove everything that's not a number 
var s1 = s0.replace(/[^0-9]/g,''); 

// A dash every three numbers 
var s2 = s1.replace(/(...)/g,'$1\-'); 

// Remove the traling slash if any 
var s3 = s2.replace(/\-$/,'');