Вы можете использовать функцию в качестве второго аргумента метода replace
, чтобы использовать toLower/toUpperCase
функции с регулярным выражением:
document.getElementById('generate').addEventListener('click', createPassword)
function createPassword(){
var len= document.getElementById('length').value;
var password=''
for(let i=0; i<len; i++){
var random= Math.floor(Math.random()*94)+33
password+= String.fromCharCode(random)
}
document.getElementById('result').innerText="Generated: "+ password;
var uppercase= document.getElementById('uppercase')
var modified;
console.clear(); // just to clear console if retried.
if (uppercase.checked){
modified = password.replace(/[a-z]/g, function(match) {return match.toUpperCase(); });
} else {
modified = password.replace(/[A-Z]/g, function(match) {return match.toLowerCase(); });
}
console.log("Modified:", modified)
}
<input type="text" id="length" value ="10"/>
<input type="button" value="generate" id="generate"/>
<input type="checkbox" id="uppercase"/>
<p id="result"></p>