Reputation: 19933
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
Upvotes: 3
Views: 388
Reputation: 522797
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 endThen, we replace with empty string, leaving behind the string we want.
Upvotes: 2
Reputation: 627600
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]);
}
Upvotes: 2
Reputation: 1149
([^\/]+)[.][^:]+:\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.
Upvotes: 1