정규식 - URL Parse
URL Parse
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 >URL를 입력해 보세요.</label>
<input type="text" class="form-control" v-model="url" />
</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.key}}</span>
</div>
<input type="text" class="form-control" :value="item.value" readonly />
</div>
</div>
</form>
</div>
CSS
html,
body {
background-color: #f8f9fa;
}
Vue
new Vue({
el: "#app",
data: {
url: "https://user:[email protected]:8080/first?a=1#hash",
attr: [],
},
computed: {
output() {
const prop = ["fullpath", "protocol", "username", "password", "host", "port", "pathname", "querystring", "fragment"].map(v => {
return {
key: v,
value: '',
}
});
const regex = /^((\w+):)?\/\/((\w+)?(:(\w+))?@)?([^\/\?:]+)(:(\d+))?(\/?([^\/\?#][^\?#]*)?)?(\?([^#]+))?(#(\w*))?/g;
const res = this.match(regex, this.url);
if (res.length) {
const position = [2, 4, 6, 7, 9, 11, 13];
const arr = [];
for (let i = 0, len = position.length; i < len; i++) {
arr.push(res[position[i]]);
}
return prop.map((v,i)=>{
v.value = arr[i];
return v;
})
}
return prop;
}
},
methods: {
match(regex, str) {
let matched = [];
let m;
while ((m = regex.exec(str)) !== null) {
matched = matched.concat(m);
}
return matched;
}
},
mounted() {}
})