RaceBase
RaceBase

Reputation: 18848

Regex Match value in Typescript

I am trying to extract the value of certain pattern from the text.

Sample text:

Test: []
subtests: [a]

I want to extract the line subtests: [a] or precisely what's the data inside [] of subtests. When I try to match the regex, it's giving wrong value. Not sure what I am doing wrong here.

https://jsfiddle.net/k8e9hu0e/2/

Can anyone help me out?

Upvotes: 2

Views: 14295

Answers (2)

soonsuweb
soonsuweb

Reputation: 105

Here is a live demo. Forked and modified from your source. https://jsfiddle.net/soonsuweb/aj38617b/

var data = `blur blur subtests: [] blur\nblur`;
var regex = /subtests: \[.*\]/;
var test = regex.exec(data);
alert("Op: " + test);

Upvotes: 1

Rajshekar Reddy
Rajshekar Reddy

Reputation: 18987

Here is a Working Fiddle. So the only change was to remove the captures ie: changing (.*) to .*

Explaining your problem..

This regex ^subtests: (.*) has captures in it. And when you find the matches for this regex, it gives you a set of all the regex matches and then all the capture's. So the first set is subtests: [] and then the set of captures that is []. Hence your output was subtests: [],[] (note the ,).

Upvotes: 1

Related Questions