I have string like this in javascript
at LoggerService.log (/Users/apps/api/webpack:/pcs-goc-api/pcs-libs/logger/src/logger.service.ts:107:29)
I want to extract logger.service from it. the formula is between last / to last .
I can extract from last / using /([^\/] $)/g but don't know how to limit the finding to last .
Note: these are other examples:
at LoggerService.log (/Users/apps/api/webpack:/pcs-goc-api/pcs-libs/logger/src/logger.ts:107:29)
expected: logger
at LoggerService.log (/Users/apps/api/webpack:/pcs-goc-api/pcs-libs/logger/src/logger.js:107:29)
expected: logger
at LoggerService.log (/Users/apps/api/webpack:/pcs-goc-api/pcs-libs/logger/src/api.logger.service.ts:107:29)
expected: api.logger.service
CodePudding user response:
You can use
/.*\/(.*)\./
Details:
.*- any zero or more chars other than line break chars as many as possible\/- a/char(.*)- Group 1: any zero or more chars other than line break chars as many as possible\.- a.char.
See the JavaScript demo:
const text = "at LoggerService.log (/Users/apps/api/webpack:/pcs-goc-api/pcs-libs/logger/src/api.logger.service.ts:107:29)";
const match = text.match(/.*\/(.*)\./)
if (match) {
console.log(match[1]);
}
CodePudding user response:
We can try using a regex replacement approach here:
var log = "at LoggerService.log (/Users/apps/api/webpack:/pcs-goc-api/pcs-libs/logger/src/api.logger.service.ts:107:29)";
var output = log.replace(/^.*\/|\.[^.]*$/g, "");
console.log(output);
The regex pattern here says to match:
^.*\/all content from the start up to and including the LAST /|OR\.[^.]*$all content from the LAST dot until the end
Then, we replace with empty string, leaving behind the string we want.
CodePudding user response:
([^\/] )[.][^:] :\d :\d
Demo: https://regex101.com/r/FeLRmi/1
[.]=> "." character[^:]=> Any character except ":"[^:]=> One or more[^:]:\d=> One or more digits after ":"
All the other regex statements are your own.
