红宝石的正则表达式在单词之前用双引号引起来 [英] Regex in ruby to enclose word before : with double quotes

查看:76
本文介绍了红宝石的正则表达式在单词之前用双引号引起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要转换为json字符串的字符串

I have a string which I want to convert in json string

以下是json的格式

               title: {
                    position: "bottom",
                    text: "Share of Internet Population Growth"
                },
                legend: {
                    visible: false
                },
                chartArea: {
                    background: ""
                },
                seriesDefaults: {
                    type: "donut",
                    startAngle: 150
                }

要将其转换为JSON字符串,我需要将AnyKey:替换为"AnyKey":
我在读有关gsub的文章.什么是确切的正则表达式来替换这种字符串

To convert it toa JSON string i need to replace the AnyKey: with "AnyKey":
I was reading about gsub. What can be exact regex to replace this kind of string

预先感谢

推荐答案

(\w+)(?=:)

尝试一下.请参见演示.

Try this.See demo.

http://regex101.com/r/zR2tR4/3

替换为"$1".

这篇关于红宝石的正则表达式在单词之前用双引号引起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆