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

问题描述 投票:-1回答:1

我需要在正则表达式的每个匹配周围添加括号或“<>”,我已经准备好了所有的正则表达式句子。例如:

输入:

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

}

哦,对不起,如果我有很多英文写作错误,而不是英语母语者:)

javascript jquery regex
1个回答
0
投票

我在很长时间内处理了复杂的字符串操作问题......

我带来了一个“dictionnary”的想法,使替换管理更容易。我使用空格来定位字符串元素以包装<>

看看代码中的注释。 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 dictionnary = [
  {
    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 dictionnary to replace some special cases
    // It adds spaces around the replacements to ensure word separation
    dictionnary.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);
© www.soinside.com 2019 - 2024. All rights reserved.