Search and Replace
Posted 梦如影
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Search and Replace相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章
Search and Replace -freecodecamp算法题目
FCC_Intermediate Algorithm Scripting_Search and Replace