【问题标题】:Get Only Link from JSON data in React Native仅从 React Native 中的 JSON 数据获取链接
【发布时间】:2019-11-08 06:17:31
【问题描述】:

我正在获取这种格式的 JSON 数据。

orderData:"<p>Ключ VVV: 6326233</p> <p>Ссылка на скачивание <a title=\"Movie\" 
href=\"https://play.google.com/store/movies/details/The_Angry_Birds_Movie_2?id=O_RbjOHHpIs&hl=en\" 
target=\"_blank\">Movie</a></p> <p>Ссылка на <a title=\"Boss\" 
href=\"https://play.google.com/store/movies/details/The_Boss_Baby?id=Ry9HH3he- 
YQ&hl=en\">лицензию</a></p>"
;

从这里我只需要单独获取第一个链接。此链接 => https://play.google.com/store/movies/details/The_Angry_Birds_Movie_2?id=O_RbjOHHpIs&hl=en

怎么做?

【问题讨论】:

  • 它只是一个字符串,你为什么不使用正则表达式来选择第一个href值。
  • 你能给我看看吗?

标签: javascript json reactjs react-native


【解决方案1】:

使用正则表达式解决之类的问题:-

var d = {
orderData:'"<p>Ключ VVV: 6326233</p> <p>Ссылка на скачивание <a title=\"Movie\" href=\"https://play.google.com/store/movies/details/The_Angry_Birds_Movie_2?id=O_RbjOHHpIs&hl=en\" target=\"_blank\">Movie</a></p> <p>Ссылка на <a title=\"Boss\" href=\"https://play.google.com/store/movies/details/The_Boss_Baby?id=Ry9HH3he- YQ&hl=en\">лицензию</a></p>"'
}

var string = d.orderData
var regex = new RegExp('[\\s\\r\\t\\n]*([a-z0-9\\-_]+)[\\s\\r\\t\\n]*=[\\s\\r\\t\\n]*([\'"])((?:\\\\\\2|(?!\\2).)*)\\2', 'ig');
var attributes = {};
while ((match = regex.exec(string))) {
    attributes['href'] = match[3]
}
console.log(attributes)

Reference

希望对你有帮助!!!

【讨论】:

    【解决方案2】:

    是的,就像@tarzen chubgh 所说的那样,它只是一个字符串,您可以使用正则表达式来获取链接。

    假设这是你的对象

    const x = {
       link: '<img src="https://www.xasd.com.vn/getsomething?ok=1&yes=1" width=100>',
       other: 'this is not  a link'
    }
    

    这是一个例子:

     const pattern = 'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)'
     const regex = RegExp(pattern)
     for (const props in x) {
       console.log(regex.exec(x[props]))
     }
    

    互联网上有很多其他的正则表达式链接,你可以找到更多:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-12
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 2015-07-15
      • 1970-01-01
      相关资源
      最近更新 更多