Relative Path in JS
Matches relative file paths (./, ../, or path/to/file).
Try it in the JS tester →Pattern
regexJS
^(\.{1,2}\/)?([^\/\0]+\/)*[^\/\0]+$JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^(\\.{1,2}\\/)?([^\\/\\0]+\\/)*[^\\/\\0]+$", "");
const input = "./src/index.ts";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
How the pattern works
`^(\.{1,2}\/)?` optionally matches `./` or `../` prefix. `([^\/\0]+\/)*` matches zero or more directory segments. `[^\/\0]+$` matches the final file segment.
Examples
Input
./src/index.tsMatches
./src/index.ts
Input
../parent/file.jsMatches
../parent/file.js
Input
file.txtMatches
file.txt