Skip to content
Advertisement

Using regex to capture a value in a text line

I have a text line and I need capture the value “Keyword” key.

In this sample the value is “ConquestImageDate”.

With my code below I can capture the value as ConquestImageDate”.

But it has a ‘”‘ on the end.

I know I can use a replace to get rid off it.

But, I´d like to do it in the regex.

JavaScript

Advertisement

Answer

You are not matching the closing ", and as exec might yield null you can check for m first before indexing into it.

JavaScript

If there can also be unwanted whitespace chars at the end, you could making the S non greedy.

JavaScript

You are matching Keyword which is singular, but if in any case you want to match spaces or newlines as well between the double quotes

JavaScript

Regex demo

Advertisement