简体   繁体   中英

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM