Kapil Choudhary
Kapil Choudhary

Reputation: 1

how to found 2 matches in regular expression

I need a regular expression for :

<<12.txt>> <<45.txt>

I have created a regular expression :

<<.+.txt>>

But this found one match in whole string but here is 2 matches:

if anyone have solution for this problem please help me out there

Upvotes: 0

Views: 47

Answers (2)

Alex Oleksiiuk
Alex Oleksiiuk

Reputation: 338

/<<.+.txt>>/gm

g is for global (will search through entire source)

m is for multi line search support

Upvotes: 0

AnonymousSB
AnonymousSB

Reputation: 3604

Part of the issue is that the string you've specified wouldn't match because the second > is missing in <<45.txt>.

Also, you're using the . (dot) selector, and also trying to find a period. It works, but now how you think it is.

Here's the regex you want:

var regex = /<<\d+\.txt>>/g

\d matches only numbers

\. matches an actual period

/g means global, so it won't stop at the first match

Practice Regular Expressions

https://regexr.com/43bs4

Demo

var string = "<<12.txt>> <<45.txt>>";
var regex = /<<\d+\.txt>>/g;
var matches = string.match(regex);

console.log(matches);

P.S., if you actually want to match with 1 > or 2 >>, you can with:

var regex = /<<\d+\.txt>>?/g

? optionally matches the character right before it

Upvotes: 1

Related Questions