Javascript regex string between first brackets, including inside brackets

I have a line like this

[test: test1: http://localhost?test=[1,2]]
[test: test2: http://localhost?test=[2,3]]

and I want to extract the text from it below

$1 = "test1"
$2 = "http://localhost?test=[1,2]"

$1 = "test2"
$2 = "http://localhost?test=[2,3]"

I'm trying to

/\[test:(.*?):(.*?)\]/

but he comes back like that. without "]"

$2 = "http://localhost?test=[2,3"

How can I change my regular expression to get what I intend? thank.

+4
source share
2 answers

One fix for your regular expression will include ]in the second group:

/\[test:(.*?):(.*?\])\]/
// add this ------^^

Another fix would be to have your existing \]regular expression match only if it is at the end of the line:

/\[test:(.*?):(.*?)\]$/
+1
source

, URL- . , URL.

[test: test1: http://localhost?test=[1,2]]
[test: test1: http://localhost?test=[1,2]&somekey=somevalue]
[test: test1: http://localhost?test=[1,2]&answers=[1,2,3]]

. , , . , ?

:

, , : " , ".
.

, , , , , :

var str = "[test: test1: http://localhost?test=[1,2]]";
str = str.substring(1, str.length - 1);

var vals = str.split(': ');

console.log(vals[1]);
console.log(vals[2]);

, . " ", " , ":)

0

All Articles