Here is the code that I've wrote. Say there is a sentence "Hello, please follow us as @followme". What this function will do is find the word that contains the "@" then return the sentence again although the @ will then be linked. If i was join this array using .join(" ") the JSX element will display as [object, object] so as a fix i've added a space to the array every other index.
My question is does this work? Have I missed something so simple that could make my life easier, would love to know! Thanks
---- As an edit, If i didn't add the extra spaces to the array or don't use .join, then the sentence is literally one word...
const getInstagram = props => {
//Split sentence into an array
let test = props.split("@" +1).pop().split(" ");
let one;
//Get the word that contains @
test.map(word => {
if (word.includes("@")) {
one = word;
}
});
//Gets the array position
let position = test.indexOf(one);
test.splice(position);
if (position >= 0) {
let line = <a href={`https://instagram.com/${one}`}>{one}</a>
test.splice(position,0,line)
}
for(let i = 0; i < test.length; i++) {
test.splice(i++,0, " ");
}
return (
<p style={{ opacity: 1 }}>
{test}
{console.log(test)}
</p>
);
};