Javascript 一种检查字符串是否包含特定子字符串中的字符的方法
我正在做一个编码训练营,我们的目标是建立一个密码生成器,用户可以选择哪种类型的字符(小写、大写、数字和特殊字符)和长度,并为他们提供随机安全密码 除了分配的一个重要部分,即生成的密码必须包含用户选择的每个字符之外,我能够使这一切都起作用。它目前是随机抓取的,所以如果您选择所有4个条件,它们都会出现,这并不总是有保证的。我如何验证这一点Javascript 一种检查字符串是否包含特定子字符串中的字符的方法,javascript,arrays,Javascript,Arrays,我正在做一个编码训练营,我们的目标是建立一个密码生成器,用户可以选择哪种类型的字符(小写、大写、数字和特殊字符)和长度,并为他们提供随机安全密码 除了分配的一个重要部分,即生成的密码必须包含用户选择的每个字符之外,我能够使这一切都起作用。它目前是随机抓取的,所以如果您选择所有4个条件,它们都会出现,这并不总是有保证的。我如何验证这一点 const lowCaseArr = "abcdefghijklmnopqrstuvwxyz"; const upCaseArr = &quo
const lowCaseArr = "abcdefghijklmnopqrstuvwxyz";
const upCaseArr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const numeralArr = "1234567890";
const specialArr = "!@#$%^&*";
function getLength() {
while (true) {
var userLength = parseInt(prompt("How many numbers, between 8 and 128, would you like to use? (Enter 0 to cancel)"));
if (userLength == 0) {
return 0;
} else if (userLength > 128 || userLength < 8) {
alert("You must enter a number between 8-128.");
} else if (userLength <= 128 && userLength >= 8) {
alert("Great! Your have selected a password with " + userLength + " characters.");
return userLength;
}
}
}
function randChar(passwordCharacters) {
return passwordCharacters.charAt(Math.floor(Math.random() * passwordCharacters.length));
}
function makePassword(userLength, passwordCharacters) {
var securePassword = "";
for (i = 0; i < userLength; i++) {
securePassword += randChar(passwordCharacters);
}
return securePassword;
}
function generatePassword() {
var userLength = getLength();
if (userLength == 0) {
return "User Cancelled Request";
}
var passwordCharacters = "";
var askLowerCase = confirm("Would you like to include lower case characters? (a, b, c)");
if (askLowerCase !== true) {
alert("Got it. No lower case characters will be included.");
} else {
alert("Great! Your password will include lower case characters!");
passwordCharacters += lowCaseArr;
}
var askUpperCase = confirm("Would you like to include upper case characters? (A, B, C)");
if (askUpperCase !== true) {
alert("Got it. No upper case characters will be included.");
} else {
alert("Great! Your password will include upper case characters!");
passwordCharacters += upCaseArr;
}
var askNumerals = confirm("Would you like to include numeral characters? (1, 2, 3)");
if (askNumerals !== true) {
alert("Got it. No numeral characters will be included.");
} else {
alert("Great! Your password will include numeral characters!");
passwordCharacters += numeralArr;
}
var askSpecial = confirm("Would you like to include special characters? (~, !, @)");
if (askSpecial !== true) {
alert("Got it. No special characters will be included.");
} else {
alert("Great! Your password will include special characters!");
passwordCharacters += specialArr;
}
var basePassword = makePassword(userLength, passwordCharacters);
var securePassword = validateOptions(basePassword, askLowerCase, askUpperCase, askNumerals, askSpecial);
return securePassword;
}
var generateBtn = document.querySelector("#generate");
function writePassword() {
var password = generatePassword();
var passwordText = document.querySelector("#password");
passwordText.value = password;
}
generateBtn.addEventListener("click", writePassword);
您可以通过确保密码满足所有约束条件,从一开始就正确生成密码,而不是事后更改密码 我使用了您的代码片段来生成一个独立函数
makeSecurePassword()
,该函数接受几个参数:userLength
,askLowerCase
,askUpperCase
,asknumerics
,askSpecial
。它返回请求的userLength
密码,仅包含请求的字符类型。它使用您的randChar()
helper函数
var securePassword = makeSecurePassword( 10, true, true, true, true );
console.log(securePassword);
// Return a random character from passwordCharacters:
function randChar(passwordCharacters) {
return passwordCharacters.charAt(Math.floor(Math.random() * passwordCharacters.length));
}
function makeSecurePassword( userLength, askLowerCase, askUpperCase, askNumerals, askSpecial ) {
const lowCaseArr = "abcdefghijklmnopqrstuvwxyz";
const upCaseArr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const numeralArr = "1234567890";
const specialArr = "!@#$%^&*";
var password = [];
// Decide which chars to consider:
charArray = [];
if ( askLowerCase ) {
charArray.push( lowCaseArr );
}
if ( askUpperCase ) {
charArray.push( upCaseArr );
}
if ( askNumerals ) {
charArray.push( numeralArr );
}
if ( askSpecial ) {
charArray.push( specialArr );
}
let x = 0; // index into charArray
for ( var i=0; i < userLength; i++ ) {
var a = charArray[x]; // Which array of chars to look at
// Insert at random spot:
password.splice( password.length, 1, randChar( a ) );
// Pick next set of chars:
if ( ++x >= charArray.length ) {
x = 0; // Start with the first set of chars if we went past the end
}
}
return password.join(''); // Create a string from the array of random chars
}
var securePassword=makeSecurePassword(10,真,真,真,真);
console.log(securePassword);
//从passwordCharacters返回随机字符:
函数randChar(密码字符){
返回passwordCharacters.charAt(Math.floor(Math.random()*passwordCharacters.length));
}
函数makeSecurePassword(userLength、askLowerCase、askUpperCase、asknumbers、askSpecial){
const lowCaseArr=“abcdefghijklmnopqrstuvxyz”;
const upCaseArr=“abcdefghijklmnopqrstuvxyz”;
常量numeriarr=“1234567890”;
const specialar=“!@$%^&*”;
var密码=[];
//决定要考虑的角色:
charArray=[];
if(askLowerCase){
特征推力(低CASEARR);
}
如果(Askupper案例){
字符推送(向上字符推送);
}
if(ask数字){
字符推送(数字arr);
}
if(askSpecial){
特征推送(特殊推送);
}
设x=0;//索引到charArray
对于(var i=0;i=charArray.length){
x=0;//如果超过末尾,则从第一组字符开始
}
}
return password.join(“”);//从随机字符数组中创建一个字符串
}
您是否尝试过string.match()
?根据我对.match()的理解,它查找不区分大小写的字符。。。因为我有大写和小写数组,所以我不确定如何在这里实现它。可以使用正则表达式.match(/thingtomatch/I)
,这里的I
表示不区分大小写。默认情况下,如果使用不需要的泛型\w
,它才不区分大小写。您需要根据所选选项显式测试[a-z]
和[a-z]
。您可以将所需字符放在字符串的开头,然后添加不受约束的字符以达到所需长度,然后作为最后一步洗牌字符串。哦,我的天哪,谢谢!我会很快尝试一下,看看是否能达到预期的效果!JavaScript是如此的强大,但至少我认为我能理解这里的每件事是什么,哈哈
var securePassword = makeSecurePassword( 10, true, true, true, true );
console.log(securePassword);
// Return a random character from passwordCharacters:
function randChar(passwordCharacters) {
return passwordCharacters.charAt(Math.floor(Math.random() * passwordCharacters.length));
}
function makeSecurePassword( userLength, askLowerCase, askUpperCase, askNumerals, askSpecial ) {
const lowCaseArr = "abcdefghijklmnopqrstuvwxyz";
const upCaseArr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const numeralArr = "1234567890";
const specialArr = "!@#$%^&*";
var password = [];
// Decide which chars to consider:
charArray = [];
if ( askLowerCase ) {
charArray.push( lowCaseArr );
}
if ( askUpperCase ) {
charArray.push( upCaseArr );
}
if ( askNumerals ) {
charArray.push( numeralArr );
}
if ( askSpecial ) {
charArray.push( specialArr );
}
let x = 0; // index into charArray
for ( var i=0; i < userLength; i++ ) {
var a = charArray[x]; // Which array of chars to look at
// Insert at random spot:
password.splice( password.length, 1, randChar( a ) );
// Pick next set of chars:
if ( ++x >= charArray.length ) {
x = 0; // Start with the first set of chars if we went past the end
}
}
return password.join(''); // Create a string from the array of random chars
}