Reputation: 1
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:
<<12.txt>>
<<45.txt>>
if anyone have solution for this problem please help me out there
Upvotes: 0
Views: 47
Reputation: 338
/<<.+.txt>>/gm
g is for global (will search through entire source)
m is for multi line search support
Upvotes: 0
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
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