在 JavaScript 中查找十六进制代码的有效性
字符串可以被视为有效十六进制代码,如果其中不包含 0-9 和 a-f 字母表以外的字符。
例如
'3423ad' is a valid hex code '4234es' is an invalid hex code
我们需要编写一个 JavaScript 函数,该函数接受一个字符串,并检查它是否为有效的十六进制代码。
示例
代码如下 −
const str1 = '4234es';
const str2 = '3423ad';
const isHexValid = str => {
const legend = '0123456789abcdef';
for(let i = 0; i < str.length; i++){
if(legend.includes(str[i])){
continue;
};
return false;
};
return true;
};
console.log(isHexValid(str1));
console.log(isHexValid(str2));输出
控制台中的输出为 −
false true
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP