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

问题描述 投票:2回答:1

我需要有条件地添加

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

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

例子: 1.“你在7天内节省了₹1,71,252”应改为

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

以前我在正则表达式的Javascript Flavor中试过这个:

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

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

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>');

但不幸的是,这也行不通!

javascript regex
1个回答
1
投票

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

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

并替换为,

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

Demo

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>'));
}
© www.soinside.com 2019 - 2024. All rights reserved.