npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

extract-path-from-string

v1.0.0

Published

Extracting file path from a string.

Readme

extract-path-from-string

Extracting file path from a string.

Table of Contents

Install

pnpm add extract-path-from-string

# or
npm i extract-path-from-string

# or
yarn add extract-path-from-string

Usage

Get the path from a single-line comment.

import { JsTsCommentExtractor } from 'extract-path-from-string';

console.log(JsTsCommentExtractor.extract('// src/single.tsx')); // src/single.tsx
console.log(JsTsCommentExtractor.extract('~/username/abc.jsonl')) // ~/username/abc.jsonl
console.log(JsTsCommentExtractor.extract('/absolute/path/filename.ext')) // /absolute/path/filename.ext
console.log(JsTsCommentExtractor.extract('@scope/pkg/dist/index.js')) // @scope/pkg/dist/index.js

Get the path from the multi-line comment.

const comment = `/** multi env config
* dev: config/dev.tsx
* test: config/test.tsx
* prod: config/prod.tsx */`
const result = JsTsCommentExtractor.extract(comment);
console.log(result); // config/dev.tsx

const result2 = JsTsCommentExtractor.extractFromContent(comment);
console.log(result2); // [ 'config/dev.tsx' ]

const result3 = JsTsCommentExtractor.extractFromContent(comment, { findAll: true });
console.log(result3); // [ 'config/dev.tsx', 'config/test.tsx', 'config/prod.tsx' ]

In addition to these, other extractors can be used, such as HandlebarsCommentExtractor, HtmlCommentExtractor, YamlCommentExtractor, CommentExtractorFactory, and GenericExtractor. CommentExtractorFactory is a factory function for extractors. If you don't want to import a specific extractor, you can use GenericExtractor.


Match the path in the string. Essential language features to consider.

import { matchPath } from 'extract-path-from-string';

console.log(matchPath('file.txt', '*.txt'));    // [ 'file.txt' ]
console.log(matchPath('file.txt', ['*.txt']))   // [ 'file.txt' ]

API

All *Extractor classes implement the Extractor interface.

export interface Extractor {
  /** Extract file paths from a single comment */
  extract(comment: string): string | null;
  /** Extract all file paths from the file content */
  extractFromContent(content: string, options?: ExtractOptions): string[];
}

export interface ExtractOptions {
  /** Whether to find all matches (default: false, only return the first one) */
  findAll?: boolean;
  /** Maximum number of lines to search (default: unlimited) */
  maxLines?: number;
  /**
   * Regular expression flags https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#advanced_searching_with_flags
   */
  flags?: string;
}

Features

  • ✅ Supports single-line comments (//, #, //!, {{}}, <!-- -->)
  • ✅ Supports multi-line comments (/* */, {{}}, <!-- -->)
  • ✅ Supports relative paths, absolute paths, and home directory paths
  • ✅ Supports npm scoped packages (@scope/package)
  • ✅ Supports multi-level file extensions (.d.ts, .spec.ts)
  • ✅ Supports line and column numbers (main.go#L6-L9)
  • ✅ Supports query parameters and anchors (src/alert.html?plain=1, src/file.html?key=value#anchor)
  • ✅ Configurable search options