代码之家  ›  专栏  ›  技术社区  ›  user1837608

如何用字母表中的升序字母在每一段文字前加上前缀?

  •  0
  • user1837608  · 技术社区  · 4 年前

    我在一个文本区域中有文本,字符串的形式是问题,后面是答案,格式如下:

    THIS IS A QUESTION
    - This is the 1st answer
    
    THIS IS ANOTHER QUESTION
    - This is another answer
    
    HERE IS THE FINAL QUESTIONS
    - Oh boy we are done!
    

    A. THIS IS A QUESTION
    - This is the 1st answer
    
    B. THIS IS ANOTHER QUESTION
    - This is another answer
    
    C. HERE IS THE FINAL QUESTIONS
    - Oh boy we are done!
    

    我对javascript正则表达式没有经验,但我已经

    (/.*[\s]/gm)

    谢谢你的帮助!

    1 回复  |  直到 4 年前
        1
  •  0
  •   CertainPerformance    4 年前

    使用replacer函数跟踪最近用-start with 64(65对应于a,66对应于B等)替换的字符代码。

    const str = `THIS IS A QUESTION
    - This is the 1st answer
    
    THIS IS ANOTHER QUESTION
    - This is another answer
    
    HERE IS THE FINAL QUESTIONS
    - Oh boy we are done!`;
    
    let lastCharCode = 64;
    
    const result = str.replace(/^\b/gm, () => String.fromCharCode(++lastCharCode) + '. ');
    console.log(result);