【问题标题】:Remove string from string using regex and return part of removed使用正则表达式从字符串中删除字符串并返回已删除的部分
【发布时间】:2023-01-31 22:04:15
【问题描述】:

我正在尝试从字符串中提取组件名称,以便我可以使用 JSON 解析其余部分并返回组件名称。

假设我有一个字符串如下:

namespace/Slider {"showCaptions":true,"circular":false,"autoPlay":true,"showItemNavigators":true,"showItemNavigatorsOnHover":true,"showIndicators":false,"indicatorsPosition":"bottom","showThumbnails":true,"numVisible":10,"numVisible1536":8,"numVisible1280":6,"numVisible1024":4,"numVisible768":3,"numVisible640":2,"changeItemOnIndicatorHover":false,"images":[{"id":1701,"url":"http://localhost:10019/wp-content/uploads/2022/12/horse.jpeg","alt":"horse alt","caption":"Name some name"},{"id":1699,"url":"http://localhost:10019/wp-content/uploads/2022/12/GPJNews_Mongolia_KK_RidingChild_076_web-920x613-1.jpeg","alt":"","caption":"Name GP"},{"id":1238,"url":"http://localhost:10019/wp-content/uploads/2022/08/test-scaled.jpeg","alt":"","caption":""},{"id":458,"url":"http://localhost:10019/wp-content/uploads/2022/09/news-scaled-1.jpeg","alt":"","caption":"Paswan a mother who..."}]}

我想从字符串中删除“命名空间/滑块”部分并返回其余部分,这样我就可以 JSON.parse 它并且还想返回“滑块”,所以命名空间之后的部分作为变量。

【问题讨论】:

标签: javascript regex


【解决方案1】:

从第一个 { 字符开始获取子字符串。

let str = 'namespace/Slider {"showCaptions":true,"circular":false,"autoPlay":true,"showItemNavigators":true,"showItemNavigatorsOnHover":true,"showIndicators":false,"indicatorsPosition":"bottom","showThumbnails":true,"numVisible":10,"numVisible1536":8,"numVisible1280":6,"numVisible1024":4,"numVisible768":3,"numVisible640":2,"changeItemOnIndicatorHover":false,"images":[{"id":1701,"url":"http://localhost:10019/wp-content/uploads/2022/12/horse.jpeg","alt":"horse alt","caption":"Name some name"},{"id":1699,"url":"http://localhost:10019/wp-content/uploads/2022/12/GPJNews_Mongolia_KK_RidingChild_076_web-920x613-1.jpeg","alt":"","caption":"Name GP"},{"id":1238,"url":"http://localhost:10019/wp-content/uploads/2022/08/test-scaled.jpeg","alt":"","caption":""},{"id":458,"url":"http://localhost:10019/wp-content/uploads/2022/09/news-scaled-1.jpeg","alt":"","caption":"Paswan a mother who..."}]}';
let idx = str.indexOf('{');
console.log(str.slice(str.indexOf('/') + 1, idx));
console.log(JSON.parse(str.slice(idx)));

【讨论】:

    【解决方案2】:

    琐碎 - 从开始到第一个替换非贪婪的一切{

    const str = `namespace/Slider {"showCaptions":true,"circular":false,"autoPlay":true,"showItemNavigators":true,"showItemNavigatorsOnHover":true,"showIndicators":false,"indicatorsPosition":"bottom","showThumbnails":true,"numVisible":10,"numVisible1536":8,"numVisible1280":6,"numVisible1024":4,"numVisible768":3,"numVisible640":2,"changeItemOnIndicatorHover":false,"images":[{"id":1701,"url":"http://localhost:10019/wp-content/uploads/2022/12/horse.jpeg","alt":"horse alt","caption":"Name some name"},{"id":1699,"url":"http://localhost:10019/wp-content/uploads/2022/12/GPJNews_Mongolia_KK_RidingChild_076_web-920x613-1.jpeg","alt":"","caption":"Name GP"},{"id":1238,"url":"http://localhost:10019/wp-content/uploads/2022/08/test-scaled.jpeg","alt":"","caption":""},{"id":458,"url":"http://localhost:10019/wp-content/uploads/2022/09/news-scaled-1.jpeg","alt":"","caption":"Paswan a mother who..."}]}`
    console.log(JSON.parse(str.replace(/^.*?{/,"{")))

    【讨论】:

      猜你喜欢
      • 2022-01-09
      • 1970-01-01
      • 2021-09-07
      • 2011-03-21
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 2021-01-17
      • 2014-06-20
      相关资源
      最近更新 更多