2014-04-08 83 views
1

如何通过JavaScript正则表达式替换JavaScript正则表达式和替换

<em>Rs</em> 154,451. Hello world 

<b>Rs 154,451</b>. Hello world 

/<em>Rs<\/em> [0-9,]*/ig 

是正则表达式找到<em>Rs</em> 154,451

回答

1

您可以使用()捕捉比赛,后来在用它替换:

var s = "<em>Rs</em> 154,451. Hello world"; 
s.replace(/<em>Rs<\/em>([^.]+)/, '<b>Rs$1</b>') 
+0

这个回报卢比$ 1 –

1

试试这个:

myString = "<em>Rs</em> 154,451. Hello world"; 
output = myString.replace(/<em>Rs<\/em>\s*(\d+(?:,\d+)?)/g, "<b>Rs $1</b>"); 
+0

它回来卢比$ 1 –

+0

感谢它的工作 –