What is the best approach to create a regex pattern that will identify variables enclosed in brackets across single and multiple lines?

In my Typescript project, I am working on matching all environment variables that are de-structured from process.env. This includes de-structuring on both single and multiple lines.

Consider the following examples in TS code that involve de-structuring from process.env. One example is on a single line, while the other spans across multiple lines:

File1.ts:

const { OKTA_AUDIENCE_DOMAIN, OKTA_DOMAIN = '', OKTA_WEB_CLIENT_ID, OKTA_ISSUER: issuer = '', AUTH_TWO: ISSUE_TWO = '', } = process.env;

File2.ts:

const {
  OKTA_AUDIENCE_DOMAIN,
  OKTA_DOMAIN = '',
  OKTA_WEB_CLIENT_ID,
  OKTA_ISSUER: issuer = '',
  AUTH_TWO: ISSUE_TWO = '',
} = process.env;

I have managed to create a script that matches de-structured variables on a single line. However, I want it to be able to match variables de-structured on both single and multiple lines.

This is the current script I have:

grep -Ezo '\{[^}]*\} = process.env' File1.ts

If run on File1, it should give me the expected output as follows:

{ AUTH0_AUDIENCE_DOMAIN, AUTH0_DOMAIN = '', AUTH0_WEB_CLIENT_ID, AUTH0_ISSUER: issuer = '', AUTH_TWO: ISSUE_TWO = '', } = process.env

As seen here, the script has successfully matched the environment variables being de-structured from process.env.

However, running the same script on File2.ts will return empty output:

grep -Ezo '\{[^}]*\} = process.env' File2.ts

The result is blank.

I am looking for a way to modify this script so that it can correctly match environment variables being de-structured on both single and multiple lines.

Answer №1

Give this a shot:

cat Document1.ts | tr '\n' ' ' | sed 's/  */ /g' | grep -Ezo '\{[^}]*\} = process.env'
cat Document2.ts | tr '\n' ' ' | sed 's/  */ /g' | grep -Ezo '\{[^}]*\} = process.env'

Pointers to keep in mind:

  • tr converts line breaks to spaces (modify to tr '\n\r' ' ' if using Windows)
  • sed eliminates multiple spaces
  • grep retains your initial criteria

Answer №2

If you don't mind setting up ripgrep on your system,

rg -oU '\{[^}]*\} = process\.env'

Using the -U flag allows for multiline matching.

Alternatively,

grep -oz '{[^}]*} = process\.env'
has worked well with the given input samples in my case. Keep in mind that using grep -z will result in output terminated with ASCII NUL characters. This may require additional processing of the output, such as piping to tr '\0' '\n'.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Getting started with Angular 2 using NPM version 3.10.6 and Angular CLI 1.0.0

I am having trouble when I run 'NPM start,' all I get is https://i.sstatic.net/QCViF.png Below are the files in my project: package.json { "name": "angular2-quickstart", "version": "1.0.0", // rest of the package.json file continues... } ...

I'm encountering difficulties utilizing ternary operators in TypeScript

I am struggling with ternary operators in TypeScript and need help understanding the issue. Please review the code below: const QuizQuestionContainer = ({ qa }: QuizQuestionContainerPropsType) => { const { question, option1, option2, option ...

Instantiate a fresh object using the new keyword followed by the Class constructor, and if desired,

I'm looking for a class that I can easily create new instances from and optionally assign properties using the constructor. For instance: class Person { name: string; age: number; constructor(props: {name?: string, age?: number}) { this.nam ...

Guide on how to connect several Subjects within an object literal to their corresponding Observables in another object literal

I am currently developing a class using Angular and I need to share multiple states associated with that class. To accomplish this, I have created several instances of BehaviorSubject private subjects = { a : new BehaviorSubject<A>(this.a), b ...

Enhance the capabilities of a basic object by incorporating a superclass through the creation of

I'm currently developing a library using Typescript 2.0 that can be utilized from both Typescript and JavaScript. Within the library, there is a class called Component and a function named registerComponent, both written in Typescript. My goal is to ...

Example of a floating undo bar using a dynamic function in a Vuex store module

Issue Overview Trigger the mutation or action of Vuex store module A to execute an external function. This external function can belong to another Vuex store module (e.g. B). A should store a reference to the external method (e.g. mutation or action from ...

How can I handle the different data type returned by ReactDom.render?

My main focus is on rendering Markdown. Additionally, I also need to parse HTML which is passed as a string. In this scenario, children represents the HTML passed as a string, while isParseRequired indicates if parsing is needed. import cx from 'clas ...

Compilation issues in node-modules arise following the Vue package and i18next updates

Recently, I decided to upgrade from the i18n package to the newer version called i18next in my project. However, this update led to numerous errors popping up during compilation. Fortunately, by adding 'skipLibCheck' to the compiler options in th ...

Setting up NestJs with TypeORM by utilizing environment files

In my setup, I have two different .env files named dev.env and staging.env. My database ORM is typeorm. I am seeking guidance on how to configure typeorm to read the appropriate config file whenever I launch the application. Currently, I am encountering ...

Implementing Angular 2 reactive forms checkbox validation in an Ionic application

I have implemented Angular Forms to create a basic form with fields for email, password, and a checkbox for Terms&Conditions in my Ionic application. Here is the HTML code: <form [formGroup]="registerForm" (ngSubmit)="register()" class="center"> ...

Discovering the import path of Node modules in ReactAlgorithm for determining the import path of

Software Development In my current project, I am utilizing Typescript along with React. To enhance the application, I integrated react-bootstrap-date-picker by executing yarn install react-bootstrap-date-picker. Unfortunately, there is no clear instruct ...

What are the steps to set up a dictionary with predetermined values?

My task is to create a pre-defined dictionary where the key represents a city and the value is an array of zones in that city. Here is my attempt: export const cityToZone: { [city: string]: Array<string> } = [ {city:'New York', [&apos ...

Dynamically manipulate menu items in material-ui and react by adding, removing, editing, or toggling their state

I have scoured every corner of the internet in search of an answer to this dilemma. Imagine a scenario where there is a menu located at the top right of a navigation bar, initially showcasing TWO options (1. Login. 2. Register). When a user clicks on eithe ...

converting nested object structures in typescript

I'm trying to flatten a nested object in my Loopback and Typescript controller Here's the structure of my model : export class SampleModel { id: number; code: number; guide?: string; gradeData?: string; } Take a look at this example obj ...

Can someone assist me with running queries on the MongoDB collection using Node.js?

In my mongodb collection called "jobs," I have a document format that needs to display all documents matching my query. { "_id": { "$oid": "60a79952e8728be1609f3651" }, "title": "Full Stack Java Develo ...

Retrieve the value of the object within the mysterious index loop in JavaScript

I have retrieved search results from the data, and each time the index of my search result varies. At one point, the result may appear in the 4th index, while at another time it might be in the 100th index. How can I retrieve the rank value from within t ...

The URL dispatcher uses regular expressions to match any and all patterns in the URL

I'm currently working on developing a URL dispatcher that specifically detects one word consisting of alphanumeric characters (i.e. [a-zA-Z]) and nothing else. I attempted to implement the following code, but unfortunately only the character detectio ...

Efficiently process and handle the responses from Promise.all for every API call, then save the retrieved data

Currently, I am passing three API calls to Promise.all. Each API call requires a separate error handler and data storage in its own corresponding object. If I pass test4 to Promise.all, how can I automatically generate its own error and store the data in ...

Types with conditions but no common parameter

I am looking to define my props as either type A or B. For instance export default function App() { type Checkbox = { type: "checkbox"; checked: boolean; }; type Dropdown = { type: "dropdown"; options: Array<an ...

Using ngModel to retrieve and display only the month, year, and date

Currently, I am working with an interface named Person which includes fields such as name, last name, birthday, and others. However, I am facing a confusion when it comes to the person's birthday format, as it contains some additional letters at the e ...