反正则匹配demo

index.js

let handleClick = () => {
    let inputString = document.getElementById('stringInput').value;
    document.getElementById('stringOutput').value =genRgx(inputString);
};
window.onload = () => {
    document.getElementById('createStringButton').addEventListener('click', handleClick);
};
var regexElements = {
    0 : '\\d', 1 : '\\D', 2 : '\\w', 3 : '\\W', 4 : '\\s', 5 : '\\S', 6 : '.', 7:'\\', //characters
    8 : '+', 9 : '*', 10 : '?',  13 : function(count){return '{'+count+'}';},//quantifiers
    11 : '|', 12 : function(ptrn){return '('+this.ptrn+')';}, 13: function(serial){return '\\'+this.serial;}, // logic
};
var keywordForSystem=['data','happy'];
function regex(first,middle,last){ 
    this.firstPart=first;
    this.middlePart = (middle==='') ? '':middle;  
    this.lastPart=last;
    this.full = this.firstPart+this.middlePart+this.lastPart;
   return this.full;
}
function analyzeSampleChar(char){
    if(char.charCodeAt(0) > 47 && char.charCodeAt(0) <58) return 0;
    if(char.charCodeAt(0) > 64 && char.charCodeAt(0) < 91) return 2; 
    if(char.charCodeAt(0) > 96 && char.charCodeAt(0) < 123) return 2; 
    if(char.charCodeAt(0)==10)return 1;
    return -1; 
}
function genRgx(sample){
    var sampleOld= document.getElementById('textInput').value;
    if(typeof(sample)=='string' && sample.length > 0){
        var map=[];
        for(var i=0;i<sample.length;++i){
        map[i]=0;
        }
      for(var ind=0;ind<keywordForSystem.length;ind++){
          var str=keywordForSystem[ind];
        for (let index = 0; index < sample.length; index++) {
            var i=sample.indexOf(str,index);
            var k=str.length;
                if(i!=-1&&sample.indexOf(str,index)!=sample.indexOf(str,index+1)){
                  while(k>0){
                    map[i]=1;
                         k--;
                         i++;
                          }
                       }
                    }
                }
        sample = sample.trim(); 
        sample = sample.split("");
        var rawRgx=[],index; 
        for(var i=0;i<sample.length;++i){
            index = analyzeSampleChar(sample[i]);
            if(map[i]===0){
             if(index===-1){
               rawRgx.push('\\'+sample[i]);
               continue;
                  } 
                  else if(index===1){
                    rawRgx.push('\\s+');
                    continue; 
                  }
            else{
                rawRgx.push(regexElements[index]);
                }
             }
            else if(map[i]===1){
                rawRgx.push(sample[i]);
            }
        }
        for(var i = 1, l = 0;i<rawRgx.length;){
            if(rawRgx[l] !== rawRgx[i])l=i++;
            if(rawRgx[l]===rawRgx[i]){
                if(rawRgx[l+1]==='+' || rawRgx[l+1]==='*') rawRgx.splice(i,1);
                else {rawRgx[i]=regexElements[8];++i;}
            }
        }
        var sRawRgx= rawRgx.join('');
        var genExp=regex('',sRawRgx,'');
        var regex_pattern =new RegExp(genExp);
        var res='';
        while(regex_pattern.test(sampleOld)) {
            var matched_value = regex_pattern.exec(sampleOld)[0];
            var strs=matched_value.split(/[:,|,\s+]/)
            var structuredStr='';
            //  matched_value=matched_value+' ';
            for(var i=0;i<strs.length;i++){
                structuredStr+='('+i+')'+strs[i]+' ';
            }
            res =res+structuredStr+'\n'; 
            sampleOld=sampleOld.replace(regex_pattern, '');              
       }  
       document.getElementById('arrayOutput').value =res;
       return genExp;
    }
}

test.html

<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Text to Regional Letter Emoji</title>

        <style type="text/css">
            .template {
                width: 300px;
                height: 200px;
            }
        </style>

        <script src="js/index.js" defer></script>
    </head>
    <body>
        <textArea id="stringInput" class="template" placeholder="input text"></textArea>
        <textArea id="stringOutput" class="template" placeholder="Generated expression"></textArea>
        <textArea id="textInput" class="template" placeholder="input text content"></textArea>
        <textArea id="arrayOutput" class="template" placeholder="The split string"></textArea>
        <button id="createStringButton">Regular expression generator!</button>
    </body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值