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 \b
before category?
You could shorten it by supplying an empty array if the match fails;
category = (classProp.match(/category_[a-z]+\b/) || [""])[0];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With