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>