Home > database >  Regular Expresion search and replace in javascript
Regular Expresion search and replace in javascript

Time:08-27

I have this code:

  {
    control: OneComponent.dynControl,
    instance: OneComponent.dynInstance,
    component: OneComponent,
  },
  {
    control: TwoComponent.dynControl,
    instance: TwoComponent.dynInstance,
    component: TwoComponent,
  }

and I'd like to replace all the blocks like this to the plain component class name.

  OneComponent,
  TwoComponent

My regex skills are rusty right now :(

Help please and thanks in advance!

CodePudding user response:

We can try the following find and replace using a regex-capable editor, such as Notepad :

Find:    /\s*\{.*?\bcomponent:\s*(\w ),.*?\}(,\n)?/gms
Replace: $1$2

Here is a working demo.

CodePudding user response:

Please see my comment before anything else, but from what you've shown us, the following should grab what you want:

(:\ \w Component)\.\w 

In your editor, try to ‘find and replace’ (however it's called for you), using the above. In the replace part, type $1. If you have regex-based replace, that should leave behind only the first part of what you called “the plain component class name”.

This is a very optimistic answer, there's a good chance you'll have more complex strings that wouldn't be matched.

  • Related