regular expression add double quotes around values and keys in javascript

前端 未结 4 1072
醉话见心
醉话见心 2020-12-21 04:05

i need a valid JSON format to request ES. i have a string like

{ 
time:  { 
          from:now-60d,
          mode:quick,
          to:now } 
}
4条回答
  •  遥遥无期
    2020-12-21 04:34

    maybe you can use :

    str.replace(/([a-zA-Z0-9-]+):([a-zA-Z0-9-]+)/g, "\"$1\":\"$2\"");
    

    Here is regex demo


    Note

    In the group [a-zA-Z0-9-] of characters i use alphabetical digits and a -, maybe you need other so you can use another one

提交回复
热议问题