Your previous regular expression had a flaw as it could detect a space in the given string
isLarge: aaaa bb-b ccc dd-d -ee',
. The updated
regex that works is:
(?<=^isLarge: [^']*'[^']*) (?=[^']*'[^']*$)
(?<=^isLarge: [^']*'[^']*)
Initially, it checks for a space preceded by isLarge:
followed by any characters except a quote, then a quote and more characters not being a quote.
(?=[^']*'[^']*$)
Next, it verifies if the space is succeeded by a quote.
Edit
reg = /(?<=^isLarge: [^']*'[^']*) (?=[^']*'[^']*$)/gm;
str = "isLarge: 'aaaa bb-b ccc dd-d -ee',\nisLarge: aaaa bb-b ccc dd-d -ee',\nisMedium: 'aaaa bb-b ccc dd-d -ee'";
r.test(str)
// true
str.replaceAll(r, " lg:").replaceAll("isLarge: ",'');
// "'aaaa lg:bb-b lg:ccc lg:dd-d lg:-ee',
// aaaa bb-b ccc dd-d -ee',
// isMedium: 'aaaa bb-b ccc dd-d -ee'"
If the pattern is ineffective, it may be due to the regex engine's lack of support for lookbehind with non-fixed width. In such cases, capturing [^']*'[^']*
and reusing it in the replacement scheme can resolve the issue, like this:
(?<=^isLarge:)([^']*'[^']*) (?=[^']*'[^']*$)
Then utilize $1
in the replace string to retain it, for instance: "$1 lg:"