Reputation: 4819
I'm a beginner at regex. Although I used https://www.regex101.com/r/nD8qB4/2 to check my regex, match()
isn't returning the value as I want.
js:
"[ls]Something[/ls]".match(/\[ls\](.*?)\[\/ls\]/g)[0];
Output:
[ls]Something[/ls]
What I want:
Something
Where's my problem? Here's a jsfiddle: http://jsfiddle.net/6b7a2z8d/
Upvotes: 0
Views: 282
Reputation: 254916
Your problem is caused by using g
modifier.
It disables using the capturing groups.
So you need:
g
[1]
index insteadIt returns 2 values because:
0
is the total match the whole regular expression captures1..N
matches per each capturing groupIn your case it is possible to get only one match:
(?<=\[ls]).*?(?=\[\/ls])
Major differences compared to your regex:
IMPORTANT: as it was noted in the comments below, the proposed solution will not work with JS, since its regular expression implementation does not support lookbehind assertions.
Upvotes: 3