victormejia
victormejia

Reputation: 1184

matching word in javascript up to a whitespace

I have the following string

var str = "Format-ABC-D Type-O"

I would like to get "ABC-D" out of this string. so fat I have

str.(/Format-(\w+)/i)[1]

But this just gives me "ABC". How can I match up to a whitespace?

Upvotes: 0

Views: 92

Answers (2)

golf
golf

Reputation: 354

You can trim the string up to the whitespace first, then do your thing. So

str = str.substr(0, str.indexOf(" ")) // str is "Format-ABC-D"

Upvotes: 0

Bergi
Bergi

Reputation: 664548

Match anything else than whitespaces, not just word characters:

/Format-(\S+)/i

Upvotes: 2

Related Questions