JavaScript-检查值是否为百分比?
假设以下是我们的值-
var value="97%";
要检查百分比值,请使用正则表达式。
示例
以下是代码-
var value="97%";
var result=/^\d+(\.\d+)?%$/.test(value);
if (result==true) {
console.log("The percent is="+value);
}
else
{
console.log("This is not percentage");
}
var value1="percent";
var result1=/^\d+(\.\d+)?%$/.test(value1);
if (result1==true) {
console.log("The percent is="+value1);
}
else
{
console.log("This is not percentage");
}要运行上述程序,您需要使用以下命令-
node fileName.js.
在这里,我的文件名为demo214.js。
输出结果
在控制台上的输出如下-
PS C:\Users\Amit\JavaScript-code> node demo214.js The percent is=97% This is not percentage
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志