如何像格式化代码一样格式化字符串

我想以编程方式将字符串格式化为代码样式,是否有任何内置脚本可以执行?

例:

let str = 'public class A{public static void main(String args[]){ System.out.print("Hello World");}}';

应采用以下格式:

str = '
public class A{
  public static void main(String args[]){
    System.out.print("Hello World");
  }
}';

字符串可以是任何编程语言

Note: I tried a more with regex, but unfortunately couldn't find a stable way, now I want to rely on a built-in script if exists already.

评论