JavaScript - 检查值是否为百分比?
javascriptweb developmentfront end technologyobject oriented programming更新于 2026/1/5 23:40:17
假设以下是我们的值减去
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
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

