Wasim A.
Wasim A.

Reputation: 9890

RegExp Match only with paths contains filename

I am trying to create a Regexp in JS that only look into string having files and ignore the paths which don't have a filename.

input

tanxe\src\lib\hello
tanxe\lib\hello\world\verboseBackup.js
tanxe\src\hello\verboseBackup.js
tanxe\lib\verboseBackup.js

Trying

input.match(/^tanxe.*?lib.*?\\..*/i)

Expected Output:

tanxe\lib\hello\world\verboseBackup.js
tanxe\lib\verboseBackup.js

Upvotes: 1

Views: 797

Answers (5)

Code Maniac
Code Maniac

Reputation: 37755

You can try this mate

^.*\.\w+$

Explanation

  • ^ - Anchor to start of string.
  • .* - Matches anything one or more times except newline character.
  • \. - Matches ..
  • \w+ - Matches word character one or more time.
  • $ - End of string.

Demo

Update:- In case you strictly want to match tanxe\lib and followed things only

You can try this mate

^tanxe\\lib\\.+\.\w+$

Demo

Upvotes: 2

Prashant Deshmukh.....
Prashant Deshmukh.....

Reputation: 2292

Try this one too.

 tanxe\\[a-zA-Z\\]+[.]{1}[a-zA-z]{2,3}

Upvotes: 1

ewwink
ewwink

Reputation: 19154

Your regex is work, I think you need is additional flags: g global, m multiline

var input = `tanxe\\src\\lib\\hello
tanxe\\lib\\hello\\world\\verboseBackup.js
tanxe\\src\\hello\\verboseBackup.js
tanxe\\lib\\verboseBackup.js
D:\\Program Files\\atom\\.atom\\packages\\utilities-tanxe\\lib\\abc\\verboseBackup.js`

input.match(/^.*tanxe.*?lib.*?\..*/gmi).forEach(r => console.log(r))
// start with "tanxe"
//input.match(/^tanxe.*?lib.*?\..*/gmi).forEach(r => console.log(r))

Upvotes: 1

Ahmad
Ahmad

Reputation: 12737

input.match(/^tanxe\\lib\\(\w+\\)*\w+\.\w+/gi);

See the regExr fiddle I created.

Upvotes: 1

bkis
bkis

Reputation: 2587

You might try this: tanxe\\lib.*?\.\w+
It matches paths starting with tanxe\lib and ending with a file extension.

Upvotes: 1

Related Questions