javascript - 如何使用 javascript 正则表达式从文件中提取 html 标签

标签 javascript regex

如何使用 javascript 正则表达式从文件中提取 html 标签。

我尝试使用这个正则表达式 /<.*>.*<\/.*>/s但它没有给出预期的结果。

这是我的源文件

我的源文件

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() {
  }

}

这是正则表达式的预期结果

预期结果

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

最佳答案

您可以使用非贪婪版本的 * 来升级您的正则表达式(检查 this )。此外,您的正则表达式不适用于自结束标签。检查这个:

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

Demo

关于javascript - 如何使用 javascript 正则表达式从文件中提取 html 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48420616/

相关文章:

javascript - 如何在循环中使用 selection.filter 方法将 D3 选择分组

regex - 如何检查字符串是否包含一组条件

c# - 包括 IDN 字符的域名正则表达式 c#

ruby-on-rails - 为什么Ruby的正则表达式使用\A和\z而不是^和$?

javascript - 改进getExif函数

javascript - 如何访问按钮的 aria 扩展值

javascript - 如何使用 javascript 为 webapp 添加 android 书签?

javascript - 如何用分类帐签署比特币 psbt?

php - Yii- 获取由正斜杠分隔的参数。

Javascript 正则表达式仅匹配某些网站