Javascript 在ruby中处理正则表达式

Javascript 在ruby中处理正则表达式,javascript,jquery,ruby,rhomobile,Javascript,Jquery,Ruby,Rhomobile,在我的控制器中,我从表中取出一些记录。我正在尝试将记录发送到Js并显示在我的页面中 @item_list = TransferDetail.find(:all) - Code to get data from table @item_列表的输出为 [{:source_id=>8, :object=>"11375810_0", :prefix=>"a",:unit=>"0", :description=>"xxxxx"}, {:source_id=>8, :

在我的控制器中,我从表中取出一些记录。我正在尝试将记录发送到Js并显示在我的页面中

 @item_list = TransferDetail.find(:all) - Code to get data from table
@item_列表的输出为

[{:source_id=>8, :object=>"11375810_0", :prefix=>"a",:unit=>"0", :description=>"xxxxx"}, {:source_id=>8, :object=>"11375810_1", :prefix=>"b", :unit=>"0", :description=>"yyyyy"}]
将记录发送到JS端

WebView.execute_js("replaceItemList('#{@item_list}')") 
在Js端

function replaceItemList(item_list){
  alert (item_list);
 }
结果是这样的

[{:source_id=>8, :object=>"11375810_0", :prefix=>"a",:unit=>"0", :description=>"xxxxx"}, {:source_id=>8, :object=>"11375810_1", :prefix=>"b", :unit=>"0", :description=>"yyyyy"}]
任何人都可以建议我如何在我的查看页面中显示

"{\"transferType\":\"D\", \"accountNumber\":\"132\", \"employeeId\":\"23\", \"orderedByPhone\":\"2423453453\", \"deliveryInstructions\":\"fdgfghvbn\"}"
我知道如何解析下面的内容并在查看页面中显示

"{\"transferType\":\"D\", \"accountNumber\":\"132\", \"employeeId\":\"23\", \"orderedByPhone\":\"2423453453\", \"deliveryInstructions\":\"fdgfghvbn\"}"

可以这样转换我的@item_列表值,或者让我知道一些其他建议

我的要求是将表(数据库记录)发送到前端并显示。 所以我确实喜欢下面 def传输
@item_list=TransferDetail.find(:all) 首选附件内容=获取首选附件内容 WebView.execute_js(“replaceItemDetails('#{preferred_accessories_content}')) 结束

def get_preferred_accessories_content
    preferred_accessories_content = ""
    @item_list.each do |pa|
      preferred_accessories_content+= "<tr>" + "<td> #{pa.description} </td>"+"</tr>"
    end
    preferred_accessories_content
end
def获取首选附件内容
首选附件内容=“”
@项目清单。每个do | pa|
首选附件内容+=“+”{pa.description}“+”
终止
首选配件内容
终止

这里“首选附件内容”的结果是正常的html表格行,因此我可以通过JS将该行附加到我的查看页面。

我根本不理解问题标题。在任何情况下,发送JSON而不是对象数组的字符串表示会更有意义吗?尝试使用to_JSON
WebView。执行_js(“replaceItemList('{@item\u list.to_JSON}')”)
@Joshua:如果任何字段中只有一个引号,这将失败,因此
.gsub(“,“\\”)
也是必要的。