douju3911 2019-06-18 15:14
浏览 93

我可以json_encode一个PHP数组并获取值作为JS RegExp对象?

I have some regex patterns stored as strings inside a PHP array. I need them to be outputted as JavaScript RegExp objects. Is there any way to do this by using PHP's json_encode?

Example:

$array = [
   'regex' => '/some-regex/' 
];
return json_encode( $array );

Desired output:

{"regex": /some-regex/}

Current output:

{"regex": "/some-regex/"}
  • 写回答

2条回答

  • dongwen9975 2019-06-18 15:45
    关注

    No, you can't. Because you're passing through JSON structure which just wraps-data. A string by json standard starts with " and ends with ", otherwise it's a number or an array for istance.

    I wondered a similiar thing about it. I wanted to create a function for JS but on server side with php, based on the result. Usually when you face this problem it means you've to rethink a bit the structure of your page/app.

    However as they pointed out in the comments to your question, you might find workarounds to build the object locally once the HTTP request is ended.

    Also would recommend reading this: Security issues JSON with eval

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料