由包括hashtag分割字符串并保存到数组使用jQuery? [英] Split string by hashtag and save into array with jQuery?
问题描述
我有具有一系列词语,其中一些具有主题标签的字符串变种,例如:
I have a var that has a string with a series of words, some of which have hashtags, eg:
var words = "#hashtagged no hashtag #antoherhashtag";
我想每个hashtagged单词保存到一个数组,有点像:
I want to save each hashtagged word into an array, kind of like:
var tagslistarr = words.split(' ');
但我不确定如何度日#和空间都包围的人物。
But I am unsure of how to get the characters surrounded by both the # and the space.
是否有这样做的一种特殊的方式?有没有我的意思用来标识一些这方面的ASCII字符?
Is there a special way of doing this? Is there some ASCII characters I am meant to use to identify this?
推荐答案
var words = "#hashtagged no hashtag #antoherhashtag";
var tagslistarr = words.split(' ');
var arr=[];
$.each(tagslistarr,function(i,val){
if(tagslistarr[i].indexOf('#') == 0){
arr.push(tagslistarr[i]);
}
});
console.log(arr);
tagslistarr = words.split('')
分割带空格的话,从一个新的数组
tagslistarr = words.split(' ')
splits words with spaces to from a new array
如果(tagslistarr [I] .indexOf('#')== 0)
是检查#
是的开始,如果这个条件真正
它添加入改编
阵列
if(tagslistarr[i].indexOf('#') == 0)
checks is the #
is in the beginning of the and if this condition is true
it adds it into the arr
array
这篇关于由包括hashtag分割字符串并保存到数组使用jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!