2014-09-25 72 views
0

問題是有一種方法,如.toUpperCase(),它可以識別不同情況下字符串之間可能存在空格。我研究了它,但找不到一個。這是我想要實現的。這是代碼學院的練習之一。js中是否有空格的方法?

var movie = prompt("movie please").toUpperCase(); 
var getReview = function (movie) { 
switch(movie){ 
    //case 'Toy Story 2': 
     //console.log("Great Story. Mean prospector."); 
     //break; 
    //case 'Finding Nemo': 
     console.log("Cool animation, and funny turtles."); 
     break; 
    case 'The Lion King': 
     console.log("Great songs."); 
     break; 
    default: 
     console.log("I don't know!"); 
} 

};

我使用不同的代碼通過了練習,但我只是想出於好奇而問。

回答

2

你可以刪除所有空格:

movie = prompt("movie please").toUpperCase().replace(/\s/g,""); 
switch(movie) { 
    case "TOYSTORY2": 
     // .... 
    case "THELIONKING": 
    case "LIONKING": // allow not having "the" as well 
     // .... 
} 
0

什麼標題呢?看到這個answer

function toTitleCase(str) 
{ 
    return str.replace(/\w\S*/g, function(txt){ 
     return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); 
    }); 
}