檢查字符串結尾 判斷一個字符串(str)是否以指定的字符串(target)結尾。


function confirmEnding(str, target) {
  
  var arr = str.replace(/\s+/g, "");
  var bb = arr.substr(arr.length - target.length, arr.length);
  if (bb == target) return true;
  
  return false;
}

confirmEnding("He has to give me a new name", "n");

  

confirmEnding("Bastian", "n") 應該返回 true.
 
 
confirmEnding("Connor", "n") 應該返回 false.
 
 
confirmEnding("Walking on water and developing software from a specification are easy if both are frozen", "specification") 應該返回 false.
 
 
confirmEnding("He has to give me a new name", "name") 應該返回 true.
 
 
confirmEnding("He has to give me a new name", "me") 應該返回 true.
 
 
confirmEnding("He has to give me a new name", "na") 應該返回 false.
 
 
confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain") 應該返回 false.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM