How to check @ (at) in array, and remove this symbol after checking
本问题已经有最佳答案,请猛点这里访问。
我有一个包含多个条目的数组。其中一些在开始时包含
| 1 2 3 4 5 | some string   @another string @one more string the best string string with email@mail.com | 
对于验证和分组,我使用这部分代码(目前只检查
| 1 2 3 4 5 6 7 8 9 |   if(linesArray[i] ===  '@'){ $('#test').append('<li class="string_with_at">'+linesArray[i]+' </li> '); }else{ $('#test').append('<li class="string_no_at">'+linesArray[i]+' </li> '); } | 
号
我的问题是:
函数删除位置0处的"@",并返回新格式的字符串:
| 1 2 3 4 5 | removeAt = function(s){ if(s.charAt(0) == '@') return s.substring(1); return s; } | 
号
怎么样?
| 1 2 3 4 5 6 7 8 9 10 11 | 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> '); } | 
这应该可以做到:
| 1 2 3 4 5 6 7 8 9 10 11 12 | 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); |