English 中文(简体)
Java reg reg
原标题:JavaScript regex for checking number have % sign or not

我有一个投入领域,无论是否希望投入百分之百的价值或固定数额,即必须用百分比的表示加以核对。

样本投入

12%
12.888%
12.00%
12.00001%
12.123344%

或固定数额

100
100.55252
100.254575

类似情况。

N.B:我想学习 j语......提出任何建议?

最佳回答
问题回答

以前的所有建议都是出色的。 根据你们的需要,你可以选择其中的任何一项。 我在下面增加一点,以抵消你的价值。 为满足你们的需要而修改。

function getValue(num) {
   var matches=num.match(/(^s*d+.?d*)(%)?s*$/);
   var m = false, p = false, v = 0;
   if (matches) {
      m = true;
      p = (matches[2]=="%");
      v = matches[1];
   }
   return {isMatch: m, isPercent: p, value: v};
}

var vals = "12%,12.888%,12.00%,12.00001%,12.123344%,100,100.,100.55252,100.254575,notnum".split( , );
for(var i=0;i<vals.length;i++) {
   var gv = getValue(vals[i]);
   console.log("string="+vals[i],"  isMatch="+gv.isMatch,"  isPercent="+gv.isPercent,"  value="+gv.value);
}

产出是:

string=12% isMatch=true isPercent=true value=12
string=12.888% isMatch=true isPercent=true value=12.888
string=12.00% isMatch=true isPercent=true value=12.00
string=12.00001% isMatch=true isPercent=true value=12.00001
string=12.123344% isMatch=true isPercent=true value=12.123344
string=100 isMatch=true isPercent=false value=100
string=100. isMatch=true isPercent=false value=100.
string=100.55252 isMatch=true isPercent=false value=100.55252
string=100.254575 isMatch=true isPercent=false value=100.254575
string=notnum isMatch=false isPercent=false value=0

As Anon has already suggested, you don t really need to use Regular Expressions to solve this problem. What s wrong with:

var number = "12.888%";
if(number[number.length - 1] ===  % ) {
  //processing...
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签