Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I separate tags in a string

I am currently using .split to try to split a string into different ‘tags’.

let text = "@yusra is cool @zain @chris is cool";
const myArray = text.split("@");
console.log(myArray);

The code above gives this output:

Array ["", "yusra is cool ", "zain ", "chris is cool"]

the expected output is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Array ["yusra", "zain ", "chris"]

How do I modify this to make it do what I want.

>Solution :

Instead of using split you can use match to find the tags via a regex.

let text = "@yusra is cool @zain @chris is cool";
const myArray = text.match(/@\w+/g);
console.log(myArray);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading