javascript - 如何在 jQuery/Javascript 中的每个正则表达式匹配周围添加括号?
问题描述
我需要在正则表达式中的每个匹配项周围添加括号或“<>”,我已经准备好所有正则表达式语句。例如:
输入:
int a = 0;
输出:
<int><a><=><0>
还有一件事,我正在做的是一个“翻译器”,它需要读取 C 中的算术计数并生成其令牌流。因此,例如,“=”将<assign_op>
和“;” 将<end_of_statement>
。上面的句子可以写成:
<int><a><assign_op><0>
这是我一直在处理的代码:
function translate() {
var input = 'int a = 0;' +
'\nint b = 5;' +
'\nint a = b + 5;' +
'\nint c = a1 / 1;' +
'\ndouble a = 1;' +
'\nfloat a = 0;' +
'\na = 0;' +
'\nfloat a = b + 1;' +
'\na = (b - c) * 5;';
var regex3 = new RegExp(/(((int|long int|double|long double|float)+\s*([a-zA-Z_]+\d*)*|([a-zA-Z_]+\d*))\s*=\s*(([a-zA-Z_]*|[a-zA-Z_]+\d*)*|\d*|\d+\.\d+);)|(((int|long int|double|long double|float)+\s*([a-zA-Z_]+\d*)*|([a-zA-Z_]+\d*))\s*=(\s*\(*(([a-zA-Z_]*|[a-zA-Z_]+\d*)*|\d*|\d+\.\d+)\)*\s*[+\-/*%]\s*\(*(([a-zA-Z_]*|[a-zA-Z_]+\d*)*|\d*|\d+\.\d+)\)*)*\s*;)/g);
var text = input.match(regex3);
var varTypes = ['int', 'double', 'float', 'long int', 'long double'];
var output = '';
text.forEach(line => {
varTypes.forEach(type => {
if (line.match(type))
line = line.replace(type, '<' + type + '>');
});
if (line.match(/=/g)) {
line = line.replace(/=/g, '<assign_op>')
}
if (line.match(/;/g)) {
line = line.replace(/;/g, '<end_of_statement>');
}
if (line.match(/\(/g)) {
line = line.replace(/\(/g, '<open_parenthesis>')
}
if (line.match(/\)/g)) {
line = line.replace(/\)/g, '<close_parenthesis>')
}
if (line.match(/[+\-*/%]/g)) {
line = line.replace(/[+\-*/%]/g, '<operator>')
}
if (line.match(/\+{2}/g)) {
line = line.replace(/\+{2}/g, '<operator>')
}
output += line + '\n';
});
console.log(output);
}
哦,对不起,如果我有很多英语写作错误,不是英语母语者:)
解决方案
我在你复杂的字符串操作问题上工作了很长时间......
我提出了一个“字典”的想法,使替换管理更容易。我使用空格来定位要用<
and包裹的字符串元素>
。
看看代码中的注释。代码笔
var input =
'int a = 0;' +
'\nint b = 5;' +
'\nint a = b + 5;' +
'\nint c = a1 / 1;' +
'\ndouble a = 1;' +
'\nfloat a = 0;' +
'\na = 0;' +
'\nfloat a = b + 1;' +
'\na = (b - c) * 5;' +
'\nlong int = (w - x) * 7;' + // Added to test the two words types
'\nlong double = (x - w) * 7;'; // Added to test the two words types
var dictionary = [
{
target: "long int",
replacement: "long|int" // | to ensure keeping that space, will be restored later
},
{
target: "long double",
replacement: "long|double" // | to ensure keeping that space, will be restored later
},
{
target: /=/g,
replacement: "assign_op"
},
{
target: /;/g,
replacement: "end_of_statement"
},
{
target: /\(/g,
replacement: "open_parenthesis"
},
{
target: /\)/g,
replacement: "close_parenthesis"
},
{
target: /[+\-*/%]/g,
replacement: "operator"
},
{
target: /\+{2}/g,
replacement: "operator"
}
];
function translate(input) {
//console.log(input);
// Your unchanged regex
var regex3 = new RegExp(/(((int|long int|double|long double|float)+\s*([a-zA-Z_]+\d*)*|([a-zA-Z_]+\d*))\s*=\s*(([a-zA-Z_]*|[a-zA-Z_]+\d*)*|\d*|\d+\.\d+);)|(((int|long int|double|long double|float)+\s*([a-zA-Z_]+\d*)*|([a-zA-Z_]+\d*))\s*=(\s*\(*(([a-zA-Z_]*|[a-zA-Z_]+\d*)*|\d*|\d+\.\d+)\)*\s*[+\-/*%]\s*\(*(([a-zA-Z_]*|[a-zA-Z_]+\d*)*|\d*|\d+\.\d+)\)*)*\s*;)/g);
// An array of lines created by the use of your regex
var lines_array = input.match(regex3);
//console.log(lines_array);
// The result variable
var output = '';
// Process each lines
lines_array.forEach(line => {
// Use the dictionary to replace some special cases
// It adds spaces around the replacements to ensure word separation
dictionary.forEach(translation => {
if (line.match(translation.target)) {
line = line.replace(translation.target, " "+translation.replacement+" "); // Notice the spaces
}
});
// Remove double spaces
line = line.trim().replace(/\s+/g," ");
// Use the spaces to get a word array to add the angle brackets
var words = line.split(" ");
words.forEach(word => {
output += "<"+word+">";
});
// Re-add the line return
output += '\n';
});
// Final fixes on the whole result string
output = output
.replace(/\|/g, " ") // Restore the space in the "two words types" ( was replaced by a | )
.replace(/<</g, "<") // Remove duplicate angle brackets
.replace(/>>/g, ">")
console.log(output);
}
// Run the function
translate(input);
推荐阅读
- oracle-apex-5.1 - Apex 5.1:交互式网格问题
- javascript - 2 使用 Map 函数传递给子组件的道具
- javascript - Ember - 多对多关系数据未更新
- java - Spark数据帧过滤(“where”)子句的自定义解释
- node.js - 猫鼬确保文件是唯一的
- firebase - 如何触发 Firebase Cloud Functions 的重试?
- javascript - Vue 完整日历(下一个,上一个)按钮触发
- php - 从数组中获取最小值并删除重复项
- html - 网站背景图片的问题
- javascript - jquery圈子进度| 更改圆圈进度的颜色