jmolina
jmolina

Reputation: 269

Regex javascript preceded by

I'm working with regex in order to obtain a word from a string. I have a css class which has multiple classes like: "foo foo-bar foo-bar2 baz-foo" and I want to extract the word after every "foo-" string. In this case will be an array of two elements: "bar" and "bar2".

Does anyone know any way to do it using regex or, even with a javascript function?

I tried things like: /foo-\w+/g or /foo-\w+-\w+/g but I'm not an expert of regex.

Thank you in advance,

Upvotes: 1

Views: 75

Answers (1)

Amit Joki
Amit Joki

Reputation: 59252

You are actually after the word after foo-. Use foo-(\S+) and the group 1 will contain what you want.

Upvotes: 3

Related Questions