Reputation:
var a = '{"test_cmd": "pybot -args : {\"custom-suite-name\" : \"my-1556\"}}'
b = a.match("\"custom-suite-name\" : \"([a-zA-Z0-9_\"]+)\"")
console.log(b)
I have a json string inside json string.
I like to extract the property custom-suite-name
's value using the regex.
Output must be,
my-1556
.
But the return value b
has no matches/null value.
Upvotes: 0
Views: 75
Reputation: 781004
Don't use a regexp to parse JSON, use JSON.parse
.
var obj = JSON.parse(a);
var test_cmd = obj.test_cmd;
var args_json = test_cmd.replace('pybot -args : ', '');
var args = JSON.parse(args_json);
var custom_suite = args.custom_suite_name;
Upvotes: 3
Reputation: 174706
It's better to put your regex within /
slashes. NOte that,
-
inside the character class because your value part contain hyphen also.Code:
> a.match(/"custom-suite-name" : "([a-zA-Z0-9_-]+)"/)[1]
'my-1556'
> a.match(/"custom-suite-name"\s*:\s*"([a-zA-Z0-9_-]+)"/)[1]
'my-1556'
Upvotes: 1