I’m trying to extract a substring matching a given pattern from a string in Javascript.
Example:
var classProp = 'active category_games',
match = classProp.match(/category_[a-z]+\b/),
category;
if(match !== null && match.length > 0){
category = match[0];
}
Is there an easier way to acheive this? A one-liner, preferably?
Should there be a
\bbefore category?You could shorten it by supplying an empty array if the match fails;