我有一个投入领域,无论是否希望投入百分之百的价值或固定数额,即必须用百分比的表示加以核对。
样本投入
12%
12.888%
12.00%
12.00001%
12.123344%
或固定数额
100
100.55252
100.254575
类似情况。
N.B:我想学习 j语......提出任何建议?
我有一个投入领域,无论是否希望投入百分之百的价值或固定数额,即必须用百分比的表示加以核对。
样本投入
12%
12.888%
12.00%
12.00001%
12.123344%
或固定数额
100
100.55252
100.254575
类似情况。
N.B:我想学习 j语......提出任何建议?
j Query使用Java的定期表述。 这是一个良好的起点:https://developer.mozilla.org/en/Jhrform/Guide/Regular_Expressions 。
您的需要,/[0-9]*? [0-9]+%/
将是有益的。
/%$/.test( "35%" ); >> true
/%$/.test( "100" ); >> false
/%$/.test( "35.555%" ); >> true
/%$/.test( "35.555" ); >> false
Take a look at Mozilla Developer Network for some more info: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp
以前的所有建议都是出色的。 根据你们的需要,你可以选择其中的任何一项。 我在下面增加一点,以抵消你的价值。 为满足你们的需要而修改。
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...
}
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.
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 ...
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 ...
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 ...
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 ...
Is it possible for someone to give me a few pointers on how to display a multidimensional array in the form of a bar graph? The array is multidimensional, with three elements in each part - and the ...
Is it possible to reload a form after file-input change? I have a form where the user can chose an image for upload. I also have a php script which displays that image resized. I only wonder if it ...
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.