ofir fridman
ofir fridman

Reputation: 2789

How to extract html tags from a file using javascript regex

How to extract html tags from a file using javascript regex.

I try with this regex /<.*>.*<\/.*>/s but it not give the expected result.

here is my source file

My Source File

import { Component, Input, OnInit, ViewEncapsulation, Output, 
EventEmitter } from '@angular/core';


@Component({
  selector: 'mgmt-ui-input',
  templateUrl: './ui-input.component.html',
  styleUrls: ['./ui-input.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class UiInputComponent implements OnInit {
  /**
   *  @example
   *  <mgmt-ui-input
   *    [showIconX]="true"
   *    [showIconV]="true"
   *    (close)="onClose($event)"
   *  >
   *    <input type="text">
   *  </mgmt-ui-input>
   *
   *
   *
   * show icon x
   * @type {boolean}
   */
  @Input() showIconX = true;
  @Input() showIconV = true;
  @Output() close = new EventEmitter<void>();

  constructor() {
  }

  ngOnInit() {
  }

}

here is the expected result from the regex

Expected Result

<mgmt-ui-input
  [showIconX]="true"
  [showIconV]="true"
  (close)="onClose($event)"
>
  <input type="text">
</mgmt-ui-input>

Upvotes: 0

Views: 135

Answers (1)

Egan Wolf
Egan Wolf

Reputation: 3573

You can upgrade your regex by using un-greedy version of * (check this). Also, your regex doesn't work with self closing tags. Check this one:

<.*?>.*?<\/.*?>|<.*?\/>

Demo

Upvotes: 2

Related Questions