0

For a string such as the following:

`const someConst= {
name: 'someName',
someProp: {
    somePropOfSomeProp: 'value'
}};`

How would i match the value of someProp ?

Assumptions:

  • the property name is always known someProp
  • the property value would always be of type object
  • The someProp object cannot have nested objects. It will be flat.

I am using Javascript regex to solve this problem.

To provide more context to the type of structure we can expect for someProp. This is basically supposed to be the bindings property for angular components. So expected values could be the following:

bindings: {
someBinding: '=',
someOtherBinding: '@'
}

OR the props for bindings may not be on the same line. So they could be:

bindings: { someBinding: '=', someOtherBinding: '@' }

So basically i would need:

{
    someBinding: '=',
    someOtherBinding: '@'
}
23
  • 1
    Why write javascript as strings, and then parse them with regex Commented Aug 25, 2017 at 16:23
  • 1
    Can there be nested objects inside the someProp object? Is the data from a safe source? If both answers are "yes", I'd be inclined to use the JS engine to evaluate the data. Commented Aug 25, 2017 at 16:25
  • 2
    @KoushikChatterjee: But that isn't valid JSON data, even without the const decl. Commented Aug 25, 2017 at 16:25
  • 1
    @adeneo I can imagine some uses, like some kind of script to act on some external code, for analysis or migration or data-mining. Commented Aug 25, 2017 at 16:25
  • 1
    I am writing a webpack loader to transform some js code before adding it to my bundle. This is the usecase. JS code is provided as string by webpack to loaders. Commented Aug 25, 2017 at 16:28

2 Answers 2

1

To use a regex to obtain the specific requirements in the question, you can use this:

/(?!bindings\s*:\s*)(?:\{[^}]*\})/

Regex101 DEMO

It uses a negative lookahead on the property name and colon so that it isn't included in the result.

However, this regex assumes there can be no } character at all before the one that is used to terminate the object. So if somewhere in that object there's a string that includes a }, it will break.

In the extended discussion, you asked how to get the full object as well. This is more complicated, and is showing that using a JS parser like Esprima is very likely going to be a much better and more reliable solution.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks i will check that parser out.
0

This regex matched the whole object including the name someProp:

/someProp: {\n\s*somePropOfSomeProp\:.*\n}/g

Use parenthesis to have a capturing group that you can use to fine-tune what you want to use. For example :

/someProp: ({\n\s*somePropOfSomeProp\:.*\n})/g

Some caveat : may fail if value string contains also somePropOfSomeProp:, I haven't test enough cases. Or if value goes on several lines.

you can check at https://regex101.com/r/AE2dNJ/4

Quick explanation :

Find someProp: { followed by a newline, followed by spaces on the same line and then somePropOfSomeProp:, then everything following until newline, and then the closing bracket on the next line.

1 Comment

Thank you for answering but i am actually looking to match the entire someProp object. If you look at my updated question you will see what i expect at the end.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.