简体   繁体   English

如何从 ReactJS 中的 api 响应返回的字符串字段中排除几个单词

[英]How to exclude a few words from a string field returned from an api response in ReactJS

im trying to exclude a few words from an api string that was returned.我试图从返回的 api 字符串中排除几个单词。 I did research on this and saw that people were using REGEX(regular expressions) for this.我对此进行了研究,发现人们为此使用了 REGEX(正则表达式)。 I tried to copy what they did, however, with api responses, it adds another level of difficulty to it and is not quite the same.我试图复制他们所做的,但是,使用 api 响应,它增加了另一个难度,并且不完全相同。

在此处输入图像描述

Is there a way to exclude the "Messari Daily Brief" from the api and include everything else?有没有办法从 api 中排除“Messari Daily Brief”并包括其他所有内容? Im trying to grab everything from that title field but the Messari Daily Text.我试图从该标题字段中获取所有内容,但 Messari Daily Text 除外。

I tried store the string like this我试着像这样存储字符串

const title = this.props.newsItems.title;

Then I tried to do the regex:然后我尝试做正则表达式:

const extractedString = '/Messari Daily Brief /'

Thanks for taking the time to read this!感谢您抽时间阅读!

you can just iterate through the items and replace that string with empty string.您可以遍历项目并将该字符串替换为空字符串。 your items will have now everything except the title does not contain that string( Messari Daily Brief )您的项目现在将拥有除标题不包含该字符串之外的所有内容( Messari Daily Brief

const items = this.props.newsItems.map(item => 
      ({ ...item, title: item.title.replace('Messari Daily Brief', '') }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM