我在react native上的应用程序上有一个数字类型的输入字段。
此字段只能接受逗号数字。
所以我用了:keyboardType="numeric"
但在这种情况下,仍然可以使用点、空格和行(-(等字符。除了输入错误数字数据时出现的任何错误之外。
正确的格式应该是:nnn,ddd
你能帮我一把吗?
let a = [
"0",//0
"0,5",//0,5
"0,,5",//0,5
"0.5",//0,5
"0..5",//0,5
".0.5",//0,5
",0,5",//0,5
"0.-5,",//0,5
"0,-5.",//0,5
"0,5-..",//0,5
"0,,,- 5,,",//0,5
"0...5- ",//0,5,
"0,5,"//0.5,
];
a.map(el=> {
const e = el
.replaceAll('.', ',')
.replace(/[^0-9,]/g, '')
console.log(e)
});
编辑:
let a = [
"0",//0
"0,5",//0,5
"0,,5",//0,5
"0.5",//0,5
"0..5",//0,5
".0.5",//0,5
",0,5",//0,5
"0.-5,",//0,5
"0,-5.",//0,5
"0,5-..",//0,5
"0,,,- 5,,",//0,5
"0...5- ",//0,5,
"00,5",//0,5
"00,05",//0,05
"05,512",//5,51
"00,5123",//0,51
",05",//0,05
"00,,5",//0,5
"21,"//21
];
- 不能有两个前导零:"00,5〃//0.5〃;00、05〃//0,05
- 如果后面跟一个没有逗号的数字,就不能有前导零:";05512"//5,51
- 逗号之后必须只有两个数字";005123"//0,51
- 最后,我认为你也可以忽略这种情况,如果字段为空,则放一个逗号或一个点,在开头放一个零,后跟一个逗号:",05〃//0,05
这里有一个与regex混合的混合字符串操作。经过编辑的条件变成了一个怪物,但现在是:
a.map(el => {
const e = el
/* normalize decimals */
.replaceAll('.', ',')
/* break into array */
.split(",")
/* add a starting zero */
.map((e,i) => i === 0 ? "0"+e : e)
/* get rid of empties (extra commas) */
.filter(e => e)
/* only work with the last 2 */
.splice(-2)
/* remove non-numbers */
.map(e => e.replace(/[^0-9.]/g, ''))
/* trim index 0 & 1 */
.map((e, i) => i === 0 ? (+e || "0") : e.substr(0, 2))
/* reassemble */
.join(",");
console.log(el, '=>', e)
});
let a = [
"0", //0
"0,5", //0,5
"0,,5", //0,5
"0.5", //0,5
"0..5", //0,5
".0.5", //0,5
",0,5", //0,5
"0.-5,", //0,5
"0,-5.", //0,5
"0,5-..", //0,5
"0,,,- 5,,", //0,5
"0...5- ", //0,5,
"00,5", //0,5
"00,05", //0,05
"05,512", //5,51
"00,5123", //0,51
",05", //0,05
"00,,5", //0,5
"21," //21
];
a.map(el => {
const e = el.replaceAll('.', ',').split(",").map((e,i) => i === 0 ? "0"+e : e).filter(e => e).splice(-2).map(e => e.replace(/[^0-9.]/g, '')).map((e, i) => i === 0 ? (+e || "0") : e.substr(0, 2)).join(",");
console.log(el, '=>', e)
});
您可以使用
let a = [
"0",//0
"0,5",//0,5
"0,,5",//0,5
"0.5",//0,5
"0..5",//0,5
".0.5",//0,5
",0,5",//0,5
"0.-5,",//0,5
"0,-5.",//0,5
"0,5-..",//0,5
"0,,,- 5,,",//0,5
"0...5- ",//0,5,
"00,5",//0,5
"00,05",//0,05
"05,512",//5,51
"00,5123",//0,51
",05",//0,05
"00,,5",//0,5
"21,"//21
];
a.map(el=> {
const e = el
.replace(/^[.,]+(?=[^.,]*$)/, '0,') // Add 0 before the first . or , if they are single
.replace(/^D+|D+$|[^0-9,.]/g, '') // remove all non-digits on both sides and non-digits except . and , in the middle
.replace(/[,.]+/g, ',') // all commas/dots to commas
.replace(/^0+(?=d)|,(?=[^,]*,)|(,d{2})d+$/g, '$1') // remove leading zeros, all commas but last, and all fractional digits after 2nd
console.log(el,'=>',e);
});
这里,
.replace(/^[.,]+(?=[^.,]*$)/, '0,')
-在第一个.
或,
之前添加0
(如果它们是字符串中唯一的逗号/点(.replace(/^D+|D+$|[^0-9,.]/g, '')
-删除字符串两侧的所有非数字和字符串中间除.
和,
以外的非数字.replace(/[,.]+/g, ',')
-用逗号替换所有逗号/点.replace(/^0+(?=d)|,(?=[^,]*,)|(,d{2})d+$/g, '$1')
-删除前导零、除最后一个逗号之外的所有逗号以及第二个小数位数之后的所有小数位数
在您的代码中,您应该允许在末尾使用逗号:
price: price
.replace(/(,[^,]*)[,.]$/, '$1') // remove trailing comma/dot if there is a comma already
.replace(/^[.,]+(?=[^.,]*$)/, '0,') // Add 0 before the first . or , if they are single
.replace(/[^0-9,.]/g, '') // remove all non-digits on both sides and non-digits except . and , in the middle
.replace(/[,.]+/g, ',') // all commas/dots to commas
.replace(/^0+(?=d)|,(?=[^,]*,)|(,d{2})d+$/g, '$1') // remove leading zeros, all commas but last, and all fractional digits after 2nd
自我解释
let a = [
"0",//0
"0,5",//0,5
"0,,5",//0,5
"0.5",//0,5
"0..5",//0,5
".0.5",//0,5
",0,5",//0,5
"0.-5,",//0,5
"0,-5.",//0,5
"0,5-..",//0,5
"0,,,- 5,,",//0,5
"0...5- ",//0,5,
"0,5,"//0.5
];
a.map(el=> {
const e = el
.replace(/D+/g, ',') // replace non digit with comma
.replace(/^,|,$/g, '') // trim the comma
console.log(e)
});