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
x
6
1
let line =
2
'(0008,0023) VERS="CQ" VR="DA" VM="1" Keyword="ConquestImageDate" Name="Conquest Image Date"';
3
const re = /Keyword="s*(S+)/;
4
m = re.exec(line);
5
console.log(m[1]);
6
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
1
7
1
let line =
2
'(0008,0023) VERS="CQ" VR="DA" VM="1" Keyword="ConquestImageDate" Name="Conquest Image Date"';
3
const re = /Keyword="s*(S+)"/;
4
m = re.exec(line);
5
if (m) {
6
console.log(m[1]);
7
}
If there can also be unwanted whitespace chars at the end, you could making the S
non greedy.
JavaScript
1
7
1
let line =
2
'(0008,0023) VERS="CQ" VR="DA" VM="1" Keyword="ConquestImageDate" Name="Conquest Image Date"';
3
const re = /Keyword="s*(S*?)s*"/;
4
m = re.exec(line);
5
if (m) {
6
console.log(m[1]);
7
}
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
1
2
1
bKeyword="s*([^"]*?)"
2