How do find the word starting with the @ symbol within a string?

+1 Joseph Mancuso · July 3, 2015
i want to find a word starting with the @ symbol. when i find the word, i want to make it into a hyperlink so the user can click on the username.

so for example, the user creates a status "hey everyone! you guys have to go checkout @username. hes so cool!"

i want to take that word that starts with the @ symbol inside the string before it goes into the database, and create a hyperlink around it so when i grab it from the database, it links directly the users profile page already. how can i do this?

Post a Reply


Oldest  Newest  Rating
0 Marius Iftime · July 8, 2015
You could simply use something like :
var pos,word,pos2;
pos = text.indexOf("@");
text = text.slice(pos);
pos2 =" ");
text = text.slice(0,pos2);

Could probably do multiple to see with delimiter is closer to to the @ ( /,/.)
0 Mahmoud Atwa · July 4, 2015

    //Assigned the value of the state 'As a string' to a variable.
    var state = 'hey everyone! you guys have to go checkout @username. hes so cool!';
    var state_array = state.split(' ');//Created an array which holds every word 'or string' that is separated by ' '.
    //the value of state_array now is :
    //[ hey , everyone! , you, guys, have, to, go, checkout, @username., hes, so, cool! ];
    //document.write(state_array[0]); just for testing every thing is working.
    //looping through the emelents of the array :
    var i;
    var array_length = state_array.length;
    for ( i=0 ; i < array_length; i++ ) {
        //indexOf() returns the position of a certain character in a given string.
            if (state_array[i].indexOf('@') == '0') {
                document.write(state_array[i] + '');

you can do something like this i guess?
Hope it helps you :D
  • 1



A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.