0
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.

1
  • There is a space before the colon in your input. Commented Sep 14, 2015 at 17:35

2 Answers 2

3

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;
Sign up to request clarification or add additional context in comments.

Comments

1

It's better to put your regex within / slashes. NOte that,

  • There is a space exists before colon.
  • No need to escape double quotes.
  • Include - 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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.