首页 > 其他 > 详细

Search and Replace

时间:2016-12-16 19:39:05      阅读:144      评论:0      收藏:0      [点我收藏+]
function myReplace(str, before, after) {
  //return str;
  if(before[0] === before[0].toUpperCase()){
    after = after[0].toUpperCase() + after.slice(1);
  }
  str = str.replace(before,after);
  return str;
}

myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

另外一种方法

function myReplace(str, before, after) {
  //return str;
  var re = /^[A-Z]/;
  if(re.test(before.charAt(0))){
    after = after.charAt(0).toUpperCase() + after.slice(1);
  }
  str = str.replace(before,after);
  return str;
}

myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

 

Search and Replace

原文:http://www.cnblogs.com/mengruying/p/6187974.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!