复杂Regex:如何穿't匹配//之后的保留字

Complex Regex : how to don't match reserved word after //

本文关键字:匹配 之后 保留字 Regex 何穿 复杂      更新时间:2023-09-26

我正在制作一个网络代码编辑器,我正在处理文本标记,所以我写了这个正则表达式:/'b(?:abstract|arguments|boolean|break|byte|case|catch|char|const|class|continue|debugger|default|delete|do|double|else|enum|eval|export|extends|false|final|finally|float|for|function|goto|if|implements|import|in|instanceof|int|interface|let|long|native|new|null|package|private|protected|public|return|short|static|super|switch|synchronized|this|throw|throws|transient|true|try|typeof|var|void|volatile|while|with|yield|alert|all|anchor|anchors|area|assign|blur|button|checkbox|clearInterval|clearTimeout|clientInformation|close|closed|confirm|constructor|crypto|decodeURI|decodeURIComponent|defaultStatus|document|element|elements|embed|embeds|encodeURI|encodeURIComponent|escape|event|fileUpload|focus|form|forms|frame|innerHeight|innerWidth|layer|layers|link|location|mimeTypes|navigate|navigator|frames|frameRate|hidden|history|image|images|offscreenBuffering|open|opener|option|outerHeight|outerWidth|packages|pageXOffset|pageYOffset|parent|parseFloat|parseInt|password|pkcs11|plugin|prompt|propertyIsEnum|radio|reset|screenX|screenY|scroll|secure|select|self|setInterval|setTimeout|status|submit|taint|text|textarea|top|unescape|untaint|window|onblur|onclick|onerror|onfocus|onkeydown|onkeypress|onkeyup|onmouseover|onload|onmouseup|onmousedown|onsubmit)'b(?=(?:[^"]*"[^"]*")*[^"]*$)(?=(?:[^']*'[^']*')*[^']*$)(?![^<]*>)(?![^'/*]*'*'/)/gm

这是一组保留字

/'b(?:abstract|arguments|boolean|break|byte|case|catch|char|const|class|continue|debugger|default|delete|do|double|else|enum|eval|export|extends|false|final|finally|float|for|function|goto|if|implements|import|in|instanceof|int|interface|let|long|native|new|null|package|private|protected|public|return|short|static|super|switch|synchronized|this|throw|throws|transient|true|try|typeof|var|void|volatile|while|with|yield|alert|all|anchor|anchors|area|assign|blur|button|checkbox|clearInterval|clearTimeout|clientInformation|close|closed|confirm|constructor|crypto|decodeURI|decodeURIComponent|defaultStatus|document|element|elements|embed|embeds|encodeURI|encodeURIComponent|escape|event|fileUpload|focus|form|forms|frame|innerHeight|innerWidth|layer|layers|link|location|mimeTypes|navigate|navigator|frames|frameRate|hidden|history|image|images|offscreenBuffering|open|opener|option|outerHeight|outerWidth|packages|pageXOffset|pageYOffset|parent|parseFloat|parseInt|password|pkcs11|plugin|prompt|propertyIsEnum|radio|reset|screenX|screenY|scroll|secure|select|self|setInterval|setTimeout|status|submit|taint|text|textarea|top|unescape|untaint|window|onblur|onclick|onerror|onfocus|onkeydown|onkeypress|onkeyup|onmouseover|onload|onmouseup|onmousedown|onsubmit)'b

如果在双引号中,则跳过标记

(?=(?:[^"]*"[^"]*")*[^"]*$)

如果在单引号中,则跳过标记

(?=(?:[^']*'[^']*')*[^']*$)

如果在标记<>中,则跳过标记

(?![^<]*>)

如果在注释/**/中,则跳过标记

(?![^'/*]*'*'/)

现在我被困在最后一块蛋糕上,如果在评论//[单行]中,我需要跳过标记

(?!'/'/['w's'''"][^'n]*)|(?!'/'/)

有什么建议吗?

我的建议是不要将正则表达式用于这种解析作业。由于您正在用Javascript构建一些东西,因此可以使用jison将您设计的语法转换为可工作的Javascript函数,该函数将根据您的语法解析文本。

如果你好奇这是我的解决方案,请告诉我你的眼睛是否在流血,或者这是一个好的解决方案:

//finding the string that I need to manipulate 
regcomment2 =/('/'/['w's'''"][^'n]*)|('/'/)/gm; 
//this the loop to find and replace
var str = finale.match(regcomment2);
    if(finale.match(regcomment2)){
        str = str.toString();
        var arr = str.split(",");
        var arrcheck = str.split(",");
        var text = "";
        var i;
    for (i = 0; i < arr.length; i++) {
    //writing right code 
        arr[i]= arr[i].replace(/(<.*?[^ok]>)/g,"");
        console.log("Commento Split Dopo = " + arr[i]);
        console.log("Commento Arr2 = " + arrcheck[i]);
    //replace original code with right code
        finale = finale.replace(arrcheck[i],arr[i]);
}