正则表达式匹配可视化工具
通过对正则表达式进行实时的测试,标注出那一部分匹配对应的另外一部分。最重要的特征就是实时性。
工具说明
实现参考
<template
>
<div
>
<div
>
<label
>Regex
:</label
> <br
/>
<textarea v
-model
="regex" style
="width: 300px"></textarea
>
</div
>
<div
>
<label
>Tests
:</label
> <br
/>
<textarea v
-model
="tests" style
="width: 500px"></textarea
>
</div
>
<div
:style
="{color: matchColor}">{{matchState
}}</div
>
<div v
-if="matches">
<div v
-for="(group,idx) in matchGroups">
<span
>group
[{{idx
}}] = {{group
}}</span
>
</div
>
</div
>
</div
>
</template
>
<script
>
module
.exports
= {
name
: "RegexMatcherLivePreview",
data(){
return {
regex
:"",
tests
:"",
result
:"",
matches
:false,
matchGroups
:[]
}
},
methods
:{
updateMatch(){
this.compiledRegex
.lastIndex
= 0
this.matches
= false
let arr
= this.compiledRegex
.exec(this.tests
)
if(arr
===null || this.compiledRegex
.lastIndex
!=this.tests
.length
){
this.matchGroups
= []
}else{
this.matches
= true
this.matchGroups
= arr
}
}
},
computed
:{
compiledRegex(){
return new RegExp(this.regex
, 'g')
},
matchState(){
return this.matches
? "Match": "Not Match"
},
matchColor(){
return this.matches
? "green" : "red"
}
},
watch
:{
'regex':function(val
){
this.updateMatch()
},
'tests':function (val
) {
this.updateMatch()
}
}
}
</script
>
<style scoped
>
</style
>
转载请注明原文地址: https://mac.8miu.com/read-485378.html