js正则校验是否中文金额
时间: 2023-11-10 21:05:48 浏览: 76
以下是一个用于校验中文金额的正则表达式:
```
/^([壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分整]|[零0])([亿]|[拾]|[佰]|[仟]){0,1}(([壹贰叁肆伍陆柒捌玖]|[零0]){1,4}|[万]){0,1}(([壹贰叁肆伍陆柒捌玖]|[零0]){1,4}|[万]){0,1}(([壹贰叁肆伍陆柒捌玖]|[零0]){1,4}|[万]){0,1}(([壹贰叁肆伍陆柒捌玖]|[零0]){1,4}){0,1}([角]|[零0]){0,1}([壹贰叁肆伍陆柒捌玖]|[零0]){0,1}([分]|[零0]){0,1}([整]|[零0]){0,1}$/
```
这个正则表达式可以匹配以下格式的中文金额:
- 壹元整
- 陆角叁分
- 壹拾元整
- 壹佰元整
- 壹仟元整
- 壹万元整
- 壹亿元整
- 壹拾万元整
- 壹佰万元整
- 壹仟万元整
- 壹亿元整
- 壹角整
- 壹分整
- 零元整
- 零角零分
- 零点零元整
使用示例:
```js
const regex = /^([壹贰叁肆伍陆柒捌玖拾佰仟万亿元角分整]|[零0])([亿]|[拾]|[佰]|[仟]){0,1}(([壹贰叁肆伍陆柒捌玖]|[零0]){1,4}|[万]){0,1}(([壹贰叁肆伍陆柒捌玖]|[零0]){1,4}|[万]){0,1}(([壹贰叁肆伍陆柒捌玖]|[零0]){1,4}|[万]){0,1}(([壹贰叁肆伍陆柒捌玖]|[零0]){1,4}){0,1}([角]|[零0]){0,1}([壹贰叁肆伍陆柒捌玖]|[零0]){0,1}([分]|[零0]){0,1}([整]|[零0]){0,1}$/;
const amount1 = "壹百贰十叁元壹角伍分";
const amount2 = "贰千元整";
const amount3 = "伍拾万元整";
const amount4 = "零元整";
const amount5 = "零点零元整";
console.log(regex.test(amount1)); // true
console.log(regex.test(amount2)); // true
console.log(regex.test(amount3)); // true
console.log(regex.test(amount4)); // true
console.log(regex.test(amount5)); // true
```
阅读全文