skip to Main Content

I want to copy past a huge list of properties rather than type them all out again. However I want to avoid copy their assignments.

The solution I’m trying involves a cmdF regex in VSCode. Then selecting all found matches.
Sample of typescript code.

    this.anchorBillingAddress = this.page.getByTestId('billing-address-section');
    this.anchorShippingAddress = this.page.getByTestId('shipping-address-section');
    this.anchorBilling = this.page.getByTestId('billing-section');
    this.anchorGeneral = this.page.getByTestId('general-section');
    this.anchorStatistics = this.page.getByTestId('statistics-section');

this[^s]+
is what I have tried. However it ends up copying the assignment as well. How do I make it stop at the first white space encountered in each line?

2

Answers


  1. You can use s*b(this.[^;]+s*;) or s*b(this.[^;]+)s*;, which captures the entire line.

    const p = /s*b(this.[^;]+s*;)/gm;
    const s = `    this.anchorBillingAddress = this.page.getByTestId(
    
    'billing-address-section'
    
    );
        this.anchorShippingAddress = this.page.getByTestId(
    
    'shipping-address-section'
    
    )
    
    ;
        this.anchorBilling = this.page.getByTestId('billing-section');
        this.anchorGeneral = this.page.getByTestId('general-section');
        this.anchorStatistics = this.page.getByTestId('statistics-section');`;
    
    const matches = s.match(p);
    if (matches) {
      matches.forEach((match, index) => {
        console.log(`${index}=> ${match}`);
      });
    }

    Note:

    • s*: 0 or more space.
    • b: word boundary.
    • (this.[^;]+s*;): captures from this. to the end of the first ;, which I assume that’s what you want.
    Login or Signup to reply.
  2. If you want it to stop at the first space, you can do this

    ^this[^s]+

    The ^ makes sure it’s from the start and won’t grab the second this.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search