开发者

jQuery extract words from string

I have a simple question. Using jQuery I want to extract words in a string to an arrary. How do I do this? An开发者_开发问答y examples?

e.g. If I have a string as shown below: I want to get only the words with the '@' prefix

var accts = "@userA @userB @userC   @userD invalidUserE @userF ";

Thanks, K.R.


You don't really need jQuery, you can do this quite simply with basic JavaScript:

var accts = "@userA @userB @userC   @userD invalidUserE @userF ";
var split = accts.split(" ");
for(var i = 0; i < split.length; i++) {
    if(split[i].charAt(0) == "@") {
      //Got one
    } 
}

You can do whatever you want to do with the strings as you find each one. You should also be able to use a regular expression.


Since you asked for jquery

var result = $.grep(accts.split(" "), function(a){ return /^@/.test(a) } )

Gives you:

["@userA", "@userB", "@userC", "@userD", "@userF"]


matches = accts.match(/(@\S+)/g)


You want to use the Javascript split function. For example:

var a = accts.split(" ");

Once you have the space-separated words in an array, iterate through the array to select the ones starting with @ as you require.


There's no need to use jQuery, it won't help in this instance. The best way to do this is to use a regular expression to identify a valid user, in this case a string beginning with the @ character. This code extract all strings starting with an @ symbol from your string:

Unlike the previous examples using split on the @ symbol it will ignore anything not starting with an @ and only return what's valid without the need for extra tests.

var accts = "@userA @userB @userC   @userD invalidUserE @userF ";
var user = null;
var patt = /@\w+/g;
while(user=patt.exec(accts))
{
    alert(user);
}


Today I have the same need for #tags also, I use above answers but i think they are not giving me the correct results when i Have strings like this

var accts = "a@userA @userB @userC   @userD invalidUserE @userF@userG ";

I have updated the above code hope it will help peoples

Demo on fiddle: http://jsfiddle.net/abdulrauf6182012/3LBsC/

code :

Html

<div class="output"></div>

Jquery:

 $(document).ready(function() {


function getHasTagAry( msgTxt, finder ) {     
     var msgTxtAry = msgTxt.split(' ');
     var hash = []
     for(var i = 0; i < msgTxtAry.length; i++) {
         if( msgTxtAry[i].indexOf(finder) > -1 )
         hash.push( msgTxtAry[i].trim() )
     }
    msgTxtAry = hash.join('').split(finder).join('-tp-'+finder).split('-tp-');
    hash = [];
    for(var i = 0; i < msgTxtAry.length; i++) {
        if( msgTxtAry[i].trim() != '' ){
         hash.push( msgTxtAry[i].trim() )         
        }

     }
    return hash;
}    

     var msgTxt = 'hellow #ABDULRAUF #RUFI #HASHTAG #HASHTAGFINDER abc #GAME';
     var hash2 = getHasTagAry( msgTxt,'#' );     

     $('.output').append( 'hash tags in string='+hash2.length+'<br/>' );
     $('.output').append( hash2.join('<br/>') );

   $('.output').append( '<br/><br/><br/>');


     var msgTxt = 'Hi @ABDULRAUF @RUFI  @username abc @AA';
     var userNames = getHasTagAry( msgTxt ,'@');     
 $('.output').append( userNames.length+'user names in str('+msgTxt+')<br/><br/>' );
 $('.output').append( userNames.join('<br/>') );



});

If any one can optimize the code it would be great :) , I have very short time for it

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜