mles mles - 2 years ago 94
Node.js Question

regex: match 'customer' in string 'styles/customer.1031.css'

I'm trying to extract the customer string out of a filepath in nodejs. So far I have come up with this:

var fileName = 'styles/customer.1031.css';
fileName = fileName.substring(7);
fileName = fileName.substring(0, fileName.length - 4);
fileName = fileName.match('[a-z]*')[0];
console.log(fileName); // <-- yields 'customer'

I'm cutting the
from the beginning and the
from the end. Then I'm only matching the lowercase characters. What would be a proper regex to match only the customer string so I don't need to cut the string before? F. ex. how would the regex look like to catch everything after
until the

Answer Source

The regex to use could look like ^styles/([^.]+)\..*$ where

  • ^styles/ translates to "starts with 'styles/'
  • Then your match (at least one character, matching until first '.')
  • Then a literal '.'
  • Then anything until the end of the string (this is optional, depending on your needs)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download