-1

I have a requirement in which I want cater operators as normal terms when they are in quotes.

For example:

input string
"acquired GE Plastics" GE Mortors

(Acquired GE Plastics) GE Mortors

The regex should match the term GE which is not wrapped in quotes/brackets. I am replacing this value to wrap it in quotes separately.Thus my resulting string will look like Results:

"acquired GE Plastics" "GE" Mortors

(Acquired GE Plastics) "GE" Mortors

And the code snippet which I'm using to get the above result is

str.replace({working-regex}, '\"GE\"');

I have two regex which serves the problem in parts. Firstly, the regex to get to match all GE terms \bGE\b and secondly the regex to match GE term wrapped in quotes ".*?(\bGE\b).*?". But I'm quite getting a way to add a not operator to exclude the match result from my second regex.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • I have two regex which serves the problem in parts. Firstly, the regex to get to match all GE terms ```\bGE\b``` and secondly the regex to match GE term wrapped in quotes ```(".*?(\bGE\b).*?")```. But I'm quite getting a way to add a not operator to exclude the match result from my second regex(I tried few suggetion on stackoverflow related to using NOT but no luck) – Rachit Rampal Mar 31 '20 at 13:50
  • So, you are replacing? Just use `s.replace(/("[^"]*")|\bGE\b/, function(x,y) { return y ? y : "replacement"; })` – Wiktor Stribiżew Mar 31 '20 at 13:56
  • I am replacing the resulting term of the entire regex match which would just be single occurrence of ```GE``` which is outside the quotes. I tried your solution but it didn't work. – Rachit Rampal Mar 31 '20 at 14:09
  • Also, the regex you suggested has | (OR) operator, whereas what i'm looking for is something related to NOT to exclude the terms which are found in the quotes – Rachit Rampal Mar 31 '20 at 14:11
  • Now, you have made it way too unclear. If you can't use code, you should explain that in the question. And you must explain what you use currently, else, we can't come up with any helpful idea. My solution works well to replace `GE` as whole word outside double quoted substrings. Just I left out `g` modifier that would deal with mutliple occurrences, it must be `/("[^"]*")|\bGE\b/g`. – Wiktor Stribiżew Mar 31 '20 at 14:29
  • Great, it worked now, Thanks – Rachit Rampal Mar 31 '20 at 14:48
  • If you want an answer please edit the question to include your own attempts you listed in the comment. You should also mention if there may be escape sequences inside double quoted substrings, too. – Wiktor Stribiżew Mar 31 '20 at 14:49
  • Sorry, I'm still learning the art of posting questions on stack overflow. I have now updated the question. If it makes better sense of what i need then can you please help with the solution. Thanks in advance – Rachit Rampal Mar 31 '20 at 15:05
  • Ah, so you also need to avoid matching also in brackets? Then you need `/("[^"]*"|\([^()]*\))|\bGE\b/` – Wiktor Stribiżew Mar 31 '20 at 15:18
  • I have not got a confirmation from you about escape sequences. Can there be `"` inside `"..."` substrings? Also, can there be other `(` and `)` inside strings between parentheses? – Wiktor Stribiżew Mar 31 '20 at 22:04
  • Hi, Thanks for your the solution, it worked. I think highly unlikely to have nested quotes and brackets but it would be great if you can help me with that advance regex as well catering nested quotes/brackets – Rachit Rampal Apr 01 '20 at 09:37

1 Answers1

-1

const message = `"acquired GE Plastics" GE Mortors`;
const capFirst = str => str.slice(0, 1).toUpperCase() + str.slice(1);
console.log(
  message.replace(/"(.+)"(.+)/g, (str, ...args) => {
    const [a, b, c] = args[0].split(" ");
    return `(${capFirst(a)} ${b.toUpperCase()} ${capFirst(c)})${args[1]}`;
  })
);
.as-console-row {color: blue!important}
xdeepakv
  • 6,631
  • 2
  • 16
  • 26