简体   繁体   English

如何在 jQuery/Javascript 中的每个 Regex 匹配项周围添加括号?

[英]How to add parentheses around every Regex match in jQuery/Javascript?

I need to add parentheses or "<>" around every match in the regex, I already got all the regex sentences ready.我需要在正则表达式中的每个匹配项周围添加括号或“<>”,我已经准备好了所有的正则表达式句子。 For example:例如:

Input:输入:

int a = 0;

Output:输出:

<int><a><=><0>

There's one more thing, what I'm doing is a "translator" it needs to read an arithmetic count in C and generate its tokens flow.还有一件事,我正在做的是一个“翻译器”,它需要读取 C 中的算术计数并生成其令牌流。 So, for example, the "=" will <assign_op> and the ";"因此,例如,“=”将<assign_op>和“;” will be <end_of_statement> .将是<end_of_statement> The sentence above would be written as: <int><a><assign_op><0>上面的句子可以写成: <int><a><assign_op><0>

Here's the code I've been working on:这是我一直在研究的代码:

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);

} }

Oh, sorry if I had many English writing mistakes, not an English native speaker :)哦,对不起,如果我有很多英语写作错误,而不是英语母语人士:)

I worked on your complex string manipulation problem quite long...我在你的复杂字符串操作问题上工作了很长时间......

I came with a "dictionary" idea make replacements management easier.我提出了一个“字典”的想法,让替换管理更容易。 And I used the spaces to target the string elements to wrap with < and > .我使用空格来定位要用<>包裹的字符串元素。

Have a look at the comments within the code.看看代码中的注释。 CodePen代码笔

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM