0

I have an array with multiple entries. Some of these contain @ at the beginning. That's an example of an array:

some string  
@another string  
@one more string  
the best string  
string with email@mail.com

For validation and grouping I use this part of code (it checked only @ for now)

  if(linesArray[i] ===  '@'){
    $('#test').append('<li class="string_with_at">'+linesArray[i]+'</li>');
  }else{
    $('#test').append('<li class="string_no_at">'+linesArray[i]+'</li>');
  }

My questions are:

  1. How can I check the @ in line start for first group?
  2. How can I remove this symbol from result ('li'+linesArray+'/li') - a may to leave only class to understand that it was an @
Michael W
  • 690
  • 1
  • 9
  • 22
sergey_c
  • 741
  • 9
  • 14

3 Answers3

1

How about that:

if(linesArray[i][0] ===  '@') { //checking the first symbol
   //remove first element from result
   $('#test').append('<li class="string_with_at">'+linesArray[i].substring(1)+'</li>');
}
else {
   $('#test').append('<li class="string_no_at">'+linesArray[i]+'</li>');
}
Artyom Neustroev
  • 8,627
  • 5
  • 33
  • 57
1

Function to remove '@' if at position 0, and return newly formatted string:

removeAt = function(s){
    if(s.charAt(0) == '@')
      return s.substring(1);
    return s;
}
Devin Lynch
  • 289
  • 2
  • 14
0

This should do the trick:

function addElement (val) {
    var match = val.match(/^(@)(.*)/),
        at    = match[1],
        str   = match[2],
        li    = $('<li/>').html(str)

    li.addClass('string_' + (at ? 'with' : 'no') + '_at');

    $('#test').append(li);
}

linesArray.forEach(addElement);
redbmk
  • 4,687
  • 3
  • 25
  • 49