2013-01-13 89 views
0

我有这个字符串,我需要从包含它们的所有单词中删除下划线数字。Javascript正则表达式来从字符串中删除字符和数字

"View":{"Name":"Untitled-4","Image_1":{"BackgroundImage":"Image.png","Position":[0,0],"Width":320,"Height":480},"Button_1":{"BackgroundImage":"ButtonTop.png","Position":[61,83],"Width":217,"Height":58},"Button_2":{"BackgroundImage":"ButtonBottom.png","Position":[81,114],"Width":205,"Height":73},"TextField_1":{"BackgroundImage":"TextFieldLogin.png","Position":[102,336],"Width":189,"Height":31},"Label_1":{"Position":[137,100],"Width":54,"Height":20,"Text":"HiRob","FontSize":18,"Color":[0,0,0,1]},"Label_2":{"Position":[43,342],"Width":72,"Height":20,"Text":"LogOut:","FontSize":18,"Color":[0,0,0,1]},"Label_3":{"Position":[115,234],"Width":126,"Height":20,"Text":"AnotherButton","FontSize":18,"Color":[0,0,0,1]}} 

我输出我期待的是:

"View":{"Name":"Untitled-4","Image":{"BackgroundImage":"Image.png","Position":[0,0],"Width":320,"Height":480},"Button":{"BackgroundImage":"ButtonTop.png","Position":[61,83],"Width":217,"Height":58},"Button":{"BackgroundImage":"ButtonBottom.png","Position":[81,114],"Width":205,"Height":73},"TextField":{"BackgroundImage":"TextFieldLogin.png","Position":[102,336],"Width":189,"Height":31},"Label":{"Position":[137,100],"Width":54,"Height":20,"Text":"HiRob","FontSize":18,"Color":[0,0,0,1]},"Label":{"Position":[43,342],"Width":72,"Height":20,"Text":"LogOut:","FontSize":18,"Color":[0,0,0,1]},"Label":{"Position":[115,234],"Width":126,"Height":20,"Text":"AnotherButton","FontSize":18,"Color":[0,0,0,1]}} 
+1

这看起来太像JSON。你确定你不想只在键上应用删除吗? – Bergi

+0

它不仅看起来像JSON,而且应用的转换将它变成_illegal_ JSON(一个JSON对象不能有重复的键,见RFC 4627) – fge

+0

这在下面解决,但是我们在那里编号的原因是要保留它在作为JSON使用时是唯一的,然后当它存储为数据库中的字符串时,我们将其编号去掉。由于其他依赖关系,我们不能以任何其他方式构建它。 – Rob

回答

2

你可以做

var replaced = str.replace(/_\d+/g, '') 
相关问题