Bonfire: Search and Replace

function myReplace(str, before, after) { if (before.charAt(0) == before.charAt(0).toUpperCase()) { after[0].toUpperCase(); console.log(after); after = after[0].toUpperCase() + after.substring(1, after.length); console.log(after); } else {after[0] = after[0].toLowerCase;} str = str.split(" "); for (i = 0; i < str.length; i ++) { if(str[i] == before) {str.splice(i, 1, after);} } str = str.join(' '); return str; } myReplace("He is Sleeping on the couch", "Sleeping", "sitting");
changer un mot par un autre en respectant la majusculation du premier

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.