I suggest you use RegExp, here is an example
var myStr = "John P White";
var matches = myStr.match(/\b(\w)/g);
alert(matches.join(''));
\b approve the position on the word boundary (^\w|\w$|\W\w|\w\W)
First capture group ( \w)
\w ( [a-zA-Z0-9_])
g : . ( )