简体   繁体   English

Javascript 检查第一个字符是数字、大写、小写还是特殊字符

[英]Javascript check if first character is number, upperCase, lowerCase or special character

The goal is a program that checks if the first character they type is a number, uppercase letter, lowercase letter or a special character/other.目标是一个程序,检查他们键入的第一个字符是数字、大写字母、小写字母还是特殊字符/其他。

Number works absolutely fine, but my task has to compare the uppercase and lowercase and check if they are letters.数字工作得很好,但我的任务必须比较大写和小写并检查它们是否是字母。 If not, it should then return special character.如果不是,则它应该返回特殊字符。 I also have to make it without using functions.我还必须在不使用函数的情况下实现它。

I am struggling to get the special character to display as it is treated as either uppercase or lowercase.我正在努力让特殊字符显示,因为它被视为大写或小写。 I have tried fiddling around with the equals signs and using regex but I am confusing myself.我试过摆弄等号和使用正则表达式,但我自己很困惑。 onlyLetters as you can see it not being used as I have tried making it == to input. onlyLetters 正如你所看到的,它没有被使用,因为我已经尝试让它 == 输入。 Doing this led to everything (except numbers) being shown as a special character and thus reversing the problem.这样做会导致所有内容(数字除外)都显示为特殊字符,从而扭转了问题。 I have also tried seeing what works for others but they do not work on uppercase and lowercase letters also like I am.我也试过看看什么对其他人有效,但他们也不像我一样对大写和小写字母有效。

Is there way to return an uppercase letter, lowercase letter or special character without the use of a function?有没有办法在不使用 function 的情况下返回大写字母、小写字母或特殊字符? Help to restore sanity is greatly appreciated非常感谢帮助恢复理智

 let input = prompt('Enter a number or uppercase or lowercase letter') let onlyLetters = /^[a-zA-Z]/ const upperCase = input.toUpperCase() const lowerCase = input.toLowerCase() const numInput = Number(input) if (Number.isInteger(numInput) == true) { console.log(input + ' is a number') } else if (input === upperCase || lowerCase) { if (input === upperCase) { console.log(input + ' is an uppercase letter') } else if (input === lowerCase) { console.log(input + ' is a lowercase letter') } } else { console.log(input + ' is a special character') }

you have an error here你这里有错误

if (input === upperCase || lowerCase) {

should be应该

if (input === upperCase || input === lowerCase) {

But just use the regex you had but did not use但是只需使用您拥有但未使用的正则表达式

 let input = prompt('Enter a number or uppercase or lowercase letter') const numInput = Number(input) if (Number.isInteger(numInput)) { console.log(input + ' is a number') } else if (input.match(/[a-zA-Z]/)) { const upperCase = input.toUpperCase() const lowerCase = input.toLowerCase() if (input === upperCase) { console.log(input + ' is an uppercase letter') } else if (input === lowerCase) { console.log(input + ' is a lowercase letter') } } else { console.log(input + ' is a special character') }

let input = prompt('Enter a number or uppercase or lowercase letter')
const upperCase = input.toUpperCase()
const lowerCase = input.toLowerCase()
const numInput = Number(input)
charCode = input.charCodeAt(0)

if (Number.isInteger(numInput) == true) {
    console.log( input + ' is a number')
}

// 65 to 90 for uppercase
// 97 to 122 for lowercase

else if (charCode >= 65 && charCode <=90) {
    console.log(input + ' is an uppercase letter')
}

else if (charCode >= 97 && charCode <=122) {
    console.log(input + ' is a lowercase letter')
}

else {
    console.log(input + ' is a special character')
}

It looks like you are trying to determine the type of the input character.看起来您正在尝试确定输入字符的类型。 Here is one way to do it without using any functions:这是一种不使用任何功能的方法:

let input = prompt('Enter a number or uppercase or lowercase letter')
let onlyLetters = /^[a-zA-Z]/

if (Number.isInteger(Number(input))) {
    console.log(input + ' is a number')
} else if (input >= 'A' && input <= 'Z') {
    console.log(input + ' is an uppercase letter')
} else if (input >= 'a' && input <= 'z') {
    console.log(input + ' is a lowercase letter')
} else {
    console.log(input + ' is a special character')
}

This solution checks if the input is a number, uppercase letter, or lowercase letter using simple comparisons.该解决方案使用简单的比较来检查输入是数字、大写字母还是小写字母。 If none of those conditions are met, it is assumed that the input is a special character.如果这些条件都不满足,则假定输入是特殊字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript正则表达式可匹配6个或更多字符实例。 (大写或小写或特殊字符或数字) - Javascript regex to match 6 or more instances of a character. (uppercase or lowercase or special character or numeric) JavaScript 将全大写字符串中的字符更改为小写 - JavaScript to change a character to lowercase in an all uppercase string 搜索大写和小写字符 - searching for character with lowercase and uppercase 正则表达式匹配列表中至少有 1 个大写字母、1 个小写字母、1 个数字和 1 个特殊字符的字符串 - Regular expression matching strings with at least 1 uppercase, 1 lowercase, 1 number and 1 special character from a list 正则表达式检查第一个字符是否为大写 - Regex to check if the first character is uppercase 如果单词的第一个字符没有以特殊字符序列开头或前缀,如何将其大写? - How to uppercase the first character of a word if it was not preceded or prefixed by a special character sequence? 用大写替换href中的小写字符 - Replace lowercase character in href with uppercase 正则表达式不起作用-至少8个字符,1个数字,1个特殊字符和1个大写字母 - Regex not working - min 8 character, 1 Number, 1 special character and 1 uppercase letter Javascript按小写字符的第一个实例拆分字符串 - Javascript split string by first instance of lowercase character Javascript检查1个特殊字符和2个数字 - Javascript check for 1 special character and 2 digits
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM