gpt4 book ai didi

ruby-on-rails - 如何从 JSON 字符串的参数中删除非文本字符、换行符、制表符等

转载 作者:数据小太阳 更新时间:2023-10-29 08:29:50 27 4
gpt4 key购买 nike

我有可能包含 \n\t 的 JSON 字符串,我不想将其保存到数据库中。 strip_tags 仅对简单字符串有帮助。我正在使用 gsub(/(\\n)|(\\t)/, "")

我想知道是否有另一种 Rails 辅助方法或更好的方法来实现这一点。

例如

"[{\"type\":\"checkbox-group\",\"label\":\"\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\nFill in\\nthe Gap (Please\\nfill in the blank box with correct wor\",\"name\":\"checkbox-group-1527245153706\",\"values\":[{\"label\":\"Option 1\",\"value\":\"option-1\",\"selected\":true}]},{\"type\":\"text\",\"label\":\"\\n\\t\\t\\n\\t\\n\\t\\n\\t\\t\\n\\t\\t\\t\\n\\t\\t\\t\\t\\n\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\t\\t\\t\\tWhat are the unique features of\\ne-commerce, digital markets, and\\ndigital goods? \\n\\t\\t\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\t\\n\\t\\t\\t\\t\\n\\t\\t\\t\\n\\t\\t\",\"className\":\"form-control\",\"name\":\"text-1527245426509\",\"subtype\":\"text\"}]"

最佳答案

您可以使用 squishsquish!

"  Some text \n\n and a tab \t\t with new line \n\n  ".squish
#=> "Some text and a tab with new line"

Squish 删除两端的所有空白字符并将剩余的空白字符分组(\n\tspace) 一个空格

关于ruby-on-rails - 如何从 JSON 字符串的参数中删除非文本字符、换行符、制表符等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50576508/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com