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 protected]

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 @
0

3 Answers 3

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>');
}
Sign up to request clarification or add additional context in comments.

2 Comments

it's better to use .charAt(0) instead of [0] for compatibility reasons
not a surprise that IE7 does not support it. thanks for your remark!
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;
}

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.