我无法从中提取网址

时间:2019-05-29 13:16:26

标签: javascript

我有长字符串,字符串会根据图像而变化,如下所示:

const TYPE_REGEX_STR = implode('|', TYPE_ARRAY);

我想在字符串的第一行和第二行中提取“ title_link”和“ /file-upload/Tq3WCcnKXYwchcMzQ/pbs.jpg”两件事。

特别是,“ title_link”将始终相同,因此这不是问题。但是“ / file-upload ...”部分将始终更改,因为它是URL。因此,我无法解决它。

非常感谢您。

1 个答案:

答案 0 :(得分:-1)

使用JSON.parse():

var string = '{"attachments":[{"ts":"1970-01-01T00:00:00.000Z","title":"pbs.jpg","title_link":"/file-upload/Tq3WCcnKXYwchcMzQ/pbs.jpg","title_link_download":true,"image_dimensions":{"width":1200,"height":630},"image_preview":"/9j/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAARACADASIAAhEBAxEB/8QAGgAAAgIDAAAAAAAAAAAAAAAAAAIEBQEDBv/EACsQAAEEAQEFBwUAAAAAAAAAAAEAAgMEEQUSEyExcgYUIjIzNDVBQlFh8P/EABYBAQEBAAAAAAAAAAAAAAAAAAUGBP/EACMRAAAFBAAHAAAAAAAAAAAAAAABAgMRBBIhMQUzNEFxcoH/2gAMAwEAAhEDEQA/AOlg1FkGuV4LET93z2scD+lYRWjqvaSerDWMIHk/B5qx1KjV7tBLH4ntcCTjIH9hQoILI1RhiDRI87QkHIBa3X1lU3IVMq1IJbYaVSWrRGN4E6OpAyRzZ5Q1wOCE+4rPYXRSjGccUj7Vc2H79oMn3OH1SSW6rW4jjGUvfVGqLcAlTHDCRs5+CZpvwl7oWrS/b1+koQp5jmF7B8ukPwKGX139RWEIVx2EIrY//9k=","image_url":"/file-upload/Tq3WCcnKXYwchcMzQ/pbs.jpg","image_type":"image/jpeg","image_size":345252,"type":"file","description":""}]}'

var object = JSON.parse(string)

console.log(object.attachments[0].title_link)