Home > Mobile >  How can I extract hastags from textinput and safe in an array in React Native
How can I extract hastags from textinput and safe in an array in React Native

Time:07-12

I want to extract hashtags from Textinput and save them in an array.

For example, the user enters

"Hello world, #Good #morning"

I want to save the #Good and #morning in an array using useState.

So I will have a result like this

hashtags = [#Good, #morning]

CodePudding user response:

I think you can simply loop through the string and in case you find a hastag(#) you can add the respective value(until the space is reached) to an array.

Or you can also use regex, like so:

const mystring = "Hello world, #Good #morning";
const hashtags = mystring.match(/#\w /g) || [];
console.log(hashtags);
  • Related