Sylar Sylar - 1 year ago 63
Javascript Question

How To Get First Word In The String

Answers on the web relates to other programming languages. I have done many searches but none seems to work. I'm looking for per title.

var str = ["bob, b", "the, d", "builder, e", "can", "he", "fix", "it" ]
str.match(/^(\w+)/) // Uncaught TypeError: str.match is not a function


I have tried to look at this but....I'm still learning and my not be using it correctly.

How do I return only
bob
and not
bob, b
?

Answer Source

JS Fiddle

var str = ["bob, b", "the, d", "builder, e", "can", "he", "fix", "it" ];
for(var i=0; i < str.length; ++i){
    console.log(str[i].match('[a-zA-Z]+'));
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download