替换字符串中的模式匹配,保留为JSON中键的值

时间:2014-06-08 03:59:08

标签: regex json string angularjs replace

我有一个JSON文件,其键值对如下所示

{
    "parameters": "<FieldLabel Type='Something'><Label><![CDATA[Click on this number to initiate call <a href='tel:123456' parameter='DialMe,100.200.3000'>100.200.3000'>100.200.3000'>100.200.3000'>tel:1002003000'>100.200.3000</a> or<a href='tel:911'parameter='dial911,911'>911'>911'>911'>tel:911'>911</a>  ]]></Label><Description><![CDATA[]]></Description></FieldLabel>"
 }

我想替换

parameter='DialMe,100.200.3000' with my-url-click='DialMe,null,null,100.200.3000'

parameter='dial911,911' with my-url-click='dial911,null,null,911' 

之前我可以使用Angular&#39; s ng-bind-html和$ sce.trustAsHtml将其呈现为HTML。 问题是JSON有许多这样的键值对,并且它们每个都有不同的参数值,如parameter=dial108,108。所以正常的字符串替换是不可能的。我应该如何为它们做每一个?

1 个答案:

答案 0 :(得分:0)

试试这个:

var str = "<FieldLabel Type='Something'><Label><![CDATA[Click on this number to initiate call <a href='tel:123456' parameter='DialMe,100.200.3000'>100.200.3000'>100.200.3000'>100.200.3000'>tel:1002003000'>100.200.3000</a> or<a href='tel:911'parameter='dial911,911'>911'>911'>911'>tel:911'>911</a>  ]]></Label><Description><![CDATA[]]></Description></FieldLabel>";

str = str.replace(/parameter=\'.*,/g, function(s){
    return s.replace("parameter", "my-url-click") + "null,null,"
});