繁体   English   中英

印度卢比 '₹' 货币的正则表达式

[英]Regex for currency of India Rupee '₹'

我需要有条件地添加

<span class="highlight"> ... </span>

在正则表达式的帮助下,在以下类型的字符串之前和之后!
类型 1: ₹ 00,00,000 或 ₹ 00.00 或 ₹ .00(INR 货币价值)
TYPE 2 : 00 kW(一些数字后跟一个空格,然后是 kW)
TYPE 4 : 00 kVA(一些数字后跟一个空格,然后是 kVA)
TYPE 3 : 000 kVAh(一些数字后跟一个空格,然后是 kVAh)

例子:
1.“您在 7 天内节省了 1,71,252 卢比”应更改为

You Saved <span class="highlight">₹ 1,71,252</span> in 7 days
  1. “节省:203 kW”更改为
Savings: <span class="highlight">203 kW</span>
  1. “消耗:225 kVAh”更改为
Consumption: <span class="highlight">225 kVAh</span>

以前我在 Javascript Flavor of regex 中尝试过这个:

this.tb1 = response["text_block_1"].replace(/(\d|₹|,|kW|kVAh|kVA)/g, '<span class="highlight">$1</span>');

但这还不够令人满意,因为它似乎突出显示了字符串中的所有数字。 我是 regex 的新手,只是无法将所有的 regex 总结为一个。 这是我能够为货币部分编写的正则表达式

this.tb1 = " ₹ 1,71,252 in 7 days".replace(/(?=.*\d)^\₹ ?(([1-9]\d{0,2}(,\d{2,3})*)|0)?(\.\d{1,2})?$/, '<span class="highlight">$1</span>');

但不幸的是,这也不起作用!

你可以使用这个正则表达式,

(₹\s+\d+(?:,\d+)*|\d+(?:,\d+)*\s+(?:kW|kVAh?))

并替换为,

<span class="highlight">$1</span>

演示

JS代码演示,

 var arr = ['You Saved ₹ 1,71,252 in 7 days','Savings: 203 kW','Consumption: 225 kVAh'] for (s of arr) { console.log(s.replace(/(₹\\s+\\d+(?:,\\d+)*|\\d+(?:,\\d+)*\\s+(?:kW|kVAh?))/g, '<span class="highlight">$1</span>')); }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM