정규식 - 속성 추출

속성 추출

by jinam yu

HTML

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<div id="app">
  <form class="p-4">
    <div class="form-group">
      <label for="exampleInputEmail1">HTML Source</label>
      <textarea class="form-control" v-model="html"></textarea>
    </div>
    <div class="form-group">
      <div class="input-group mb-3" v-for="(item, index) in output" :key="index">
        <div class="input-group-prepend">
          <span class="input-group-text">{{item[1]}}</span>
        </div>
        <input type="text" class="form-control" :value="item[2]" readonly />
      </div>
    </div>
  </form>
</div>

CSS

html,
body {
  background-color: #f8f9fa;
}

Vue

new Vue({
  el: "#app",
  data: {
    html: '<div><a href="http://www.domain.com" target="_blank"><img src="https://www.image.com/image.jpg" alt="a" /></a></div>',
  },
  computed: {
    output() {
      let matched = [];
      const regex = /(\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))*.)["']?/g;
      while ((m = regex.exec(this.html)) !== null) {
        matched.push(m);
      }
      return matched;
    }
  },
  methods: {},
  mounted() {}
})