I'm failing to write a reg exp that catches things all occurences of { key: something }
where something
can be anything except }
or . I've tried
{ key: [^}]* }
{ key: [^} ]* }
{ key: (.*?)({| ) }
{ key: (.*?)({) }
{ key: (?!.*({| )) }
{ key: (?!.*({)) }
of these the first one works sometimes, not according to any pattern I can see. I'm using these in javascript:
const pattern = '{ key: [^}]* }';
const regExp = new RegExp(pattern, 'igm');
let match = true;
do {
match = regExp.exec(text);
if (match) {
// use result
}
} while (match);
(I've also tried adding regExp.lastIndex = 0;
, and setting regExp = null
and redefining, in the loop).
However I don't think it's (only) a javascript problem; They don't work either on the online regexp tester.
I need the character indices; as an example I would like
kjh { key: sfdg } lkk { key: a }
to yield (5, 13) and (23, 10) (might be some off by one errors there)
Any suggestions?
EDIT: If relevant, the usecase is to highlight text following this pattern in an editor, using draft.js