Skip to content

Instantly share code, notes, and snippets.

View LaurelineP's full-sized avatar

Laureline Paris LaurelineP

View GitHub Profile
@LaurelineP
LaurelineP / index.js
Created March 28, 2019 09:53
Search and replace - algorithm created by LaurelineP - https://repl.it/@LaurelineP/Search-and-replace-algorithm
function myReplace(str, before, after ) {
/*
* 1. Check if the old word has its first letter in a capital or not:
* - if yes : return the new word with a capital on the first letter
* - if no : return the new word as it is
*/
let newStr = /^[A-Z]/.test( before[0] )
? `${ after.substring( 0, 1 ).toUpperCase()}${ after.substring( 1 ) }`
: after;