Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I replace ?? with ternary operator in js?

I have a JS file which was created using browserify, and it uses the ?? operator. The problem is, the environment where I am executing this JS file doesn’t support that operator yet.

Is there any way I can go through the file, search and replace it using regex?

The change would be from this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

object.error ?? 0

To this:

x == undefined ? 0 : object.error

>Solution :

Is it JS or Python?

Anyway, try capturing the line with:

(([^ \t]+)[ \t]*\?\?[ \t]*\d+)

This expression returns two groups:

  1. The line to be replaced
  2. The object.error

You then need to replace group#1 by x == undefined ? 0 : group#2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading