用js提取json数组中的信息

有一个json数组:

const products=[
    {"name": "pear","url": "https://www.example.com/anamariat/"},
    {"name": "apple","url": "https://www.example.com.com/gutang/"},
    {"name": "fish","url": "https://www.example.com.com/rby_chis/"},
    {"name": "tin","url": "https://www.example.com.com/ndmapbysth/"},
    {"name": "water","url": "https://www.example.com.com/admkeupeph/"}
    ];

我想提取网址后面的一截,放到一个新数组中,结果像这样:

[
    "anamariat",
    "gutang",
    "rby_chis",
    "ndmapbysth",
    "admkeupeph"
]

问题:
用js实现,应该怎么做呢?

阅读 2.7k
1 个回答

分析一下:
数组提取 -> "https://www.example.com/anamariat/"提取"anamariat"

const str = "https://www.example.com/anamariat/";
str.replace(/^https:\/\/.*?\//,'').replace('/', '');// 完事.  你再遍历一遍就行

当然还有更简单的正则

'https://www.example.com/anamariat/'.match(/\/([a-z_]*)\/$/, '')[1]
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题