-4

Here are the strings like this

var a = ".fontlist-1 .fontlist-2";
var b = ".fontlist-1 .fontlist-3";
var c = ".fontlist-1 .fontlist-4";

What I need is how can I use the split() method and get only the second word in the string. For the above strings, I need only the second words( ie., .fontlist-2, .fontlist-3, .fontlist-4). How can I split and get that word? If you need any more information, I can give you.

One more thing. At some situation, If I have string like this.

var a = ".fontlist-1"

Your code must give me the .fontlist-1 word then. Will it be possible?

Could anyone guide me? Thanks!

rnk
  • 2,174
  • 4
  • 35
  • 57
  • https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split – Andreas May 06 '12 at 10:25
  • 5
    Did you read the documentation? https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/split Come on, guys, RESEARCH. – Mahmoud Al-Qudsi May 06 '12 at 10:26
  • possible duplicate of [How to use Split with jQuery?](http://stackoverflow.com/questions/2555794/how-to-use-split-with-jquery) – Felix Kling May 06 '12 at 10:40

1 Answers1

1

".fontlist-1 .fontlist-2".split(/\s+/)[1] gives you the second word of the string, in this case .fontlist-2. You could have found it yourself.

Well, what the heck ... for a, b, and c try:

[a,b,c]
   .map(function(el){return el.split(/\s+/).pop();})
   .join(' ');
//=> ".fontlist-2 .fontlist-3 .fontlist-4"
Community
  • 1
  • 1
KooiInc
  • 119,216
  • 31
  • 141
  • 177