检测链接并在本机反应中返回另一个视图

我的代码:


const orderResultJson = [

  {

    key: 'Скачайте приложение по ссылке',

    value: 'https://google.com'

  },

  {

    key: 'Логин',

    value: 'https://instagram.com'

  },

  {

    key: 'Пароль',

    value: '849846'

  },

];


function DetailsSection({ item }){

  return(

     <View>

        <Text>{item.key}</Text>

        <Text> {replaceURLWithHTMLLinks(item.value) ? 'Cсылка' : 'No'} </Text>

    </View>

  )

}

  function replaceURLWithHTMLLinks(text)

    {

        var expression =  

/[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi; 

        var regex = new RegExp(expression); 

        var url = text; 

        url.match(regex) ? true : false;



render() {

    return (

     ........

        <FlatList

          .......

          renderItem={({item}) => <DetailsSection item={item} />} 

          keyExtractor={item => item.key} 

        />

      .....

}

我在 DetailsSection 函数中有一个条件。逻辑如下:


如果 {item.value} 包含链接,则显示“一些随机文本”


如果不是,只显示什么是“否”


目前,我还没有完全理解这个问题。一切似乎都运行良好,但 DetailsSection 内的条件流仅显示没有。但是在 JSON 中,我确实有超链接。有什么建议吗?


杨__羊羊
浏览 171回答 2
2回答

红糖糍粑

也许您错过了 replaceURLWithHTMLLinks 函数中的“return”语句:...return !!url.match(regex);...

温温酱

我认为您使用的正则表达式不允许在 URL 开头使用 https:// 。而且您还忘记从函数中返回布尔值。尝试使用这个:function replaceURLWithHTMLLinks(text)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var expression =&nbsp; /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/gi;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var regex = new RegExp(expression);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var url = text;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return url.match(regex) ? true : false;}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript