Return First Character of each word in a string
I would suggest you to use RegExp
, Here's an Example
var myStr = "John P White";
var matches = myStr.match(/\b(\w)/g);
console.log(matches.join(''));
\b
assert position at a word boundary (^\w|\w$|\W\w|\w\W)
1st Capturing Group (
\w
)
\w
matches any word character (equal to[a-zA-Z0-9_]
)Global pattern flags
g
modifier: global. All matches (don't return after first match)
For better explanation visit here