Javascript 解析特定字符串的不同部分

标签 javascript regex

我正在尝试将我的 WordPress 网站转换为完全前端、REST API 驱动的网站。我正在摆脱所有的 php 并几乎使用 WordPress 作为没有 php 请求的 CMS。我有很多 WordPress 正在为我解析的代码,但我现在需要自己处理。

我需要能够将其分成几个部分:[my_social Artist =“blink-182”facebook =“https://en-gb.facebook.com/blink182/”soundcloud =“https://soundcloud.com/blink-182”spotify =“https://open.spotify.com/artist/6FBDaR13swtiWwGhX1WQsP”]

我最终希望输出如下:

{
    tag: "my_social",
    artist: "blink-182",
    facebook: "https://en-gb.facebook.com/blink182/",
    soundcloud: "https://soundcloud.com/blink-182",
    spotify: "https://open.spotify.com/artist/6FBDaR13swtiWwGhX1WQsP"
}

我尝试了正则表达式,但不太确定如何重复任何非特定的键值组。有什么帮助吗?

最佳答案

您可以使用此正则表达式来捕获您想要的内容:

(\w+)\s*=\s*(".*?")|(\w+)

<强> Working demo

然后您可以使用捕获的内容创建一个新字符串:

"\1":\2,

并连接字符串:

"tag":"\3"

关于Javascript 解析特定字符串的不同部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39760473/

相关文章:

regex - Perl 就地编辑 : Find and replace in X12850 formatted file

javascript - 相似 URL 的正则表达式模式匹配

php - 单词的正则表达式

javascript - Angular 4 : Validators. 模式()不起作用

javascript - 如何从应用了 Select2 的选择元素中删除选项?

javascript - 如何将 url 附加到我作为 json 响应获得的 div 内的 anchor 标记?

javascript - 在 js 中的更改事件上多次调用一个函数

c# - 正则表达式 - 订购多行 SQL 字符串

javascript - JIT Spacetree 将标签保存为图像

javascript - 避免在 ES6 中使用 .bind =>