I have a simple question. Using jQuery I want to extract words in a string to an arrary. How do I do this? Any 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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With