• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

203
Views
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:

object.error ?? 0

To this:

x == undefined ? 0 : object.error
almost 3 years ago · Santiago Gelvez
1 answers
Answer question

0

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

almost 3 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error