复杂Regex:如何穿't匹配//之后的保留字
Complex Regex : how to don't match reserved word after //
我正在制作一个网络代码编辑器,我正在处理文本标记,所以我写了这个正则表达式:/'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]);
}
- jQuery匹配JSON对象的部分文本
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 在chrome.tabs.onCreated之后加载HTML页面
- 复杂Regex:如何穿't匹配//之后的保留字
- 匹配点之后的任何字符,直到下一个点或行尾
- 匹配最后一个点之后的文本字符串
- 任何匹配模式的方法,要么在某个字符之前,要么在某个字符之后
- 如何匹配@之后的所有内容,直到空格
- jQuery选择器,用于查找与选择器匹配的给定元素之后的第一个元素
- 正则表达式在文本中查找匹配项,而不是在单词之后
- 使用jquery在dom的其余部分中查找特定元素之后的第一个匹配项
- javascript多行匹配在某个字符串之后
- 匹配一个“"在特定图案之后,同时排除该图案.(JavaScript中的lookbacking)
- regex匹配第一个字符之前和最后一个字符之后的空格或符号之间的字符串
- 正则表达式匹配大写字母,而不是在破折号之后
- 正则表达式,它匹配一个单词,而不是在另一个单词之后
- Javascript RegExp -如何匹配X只有当A在X之前和B在X之后
- 匹配出现在某个字符串之后的字符串
- Chrome扩展赢得't识别#(contentscript.js匹配)之后的URL部分
- 用于匹配两个/反斜杠/之间的字符串的正则表达式模式,该模式直接出现在另一个字符串的第一次匹配之后