使用while循环进行replace操作
代码示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> </body> <script> var str = '<div>{{name}}今年{{age}}</div>' var pattern = /{{\s*([a-zA-Z]+)\s*}}/ var a = null while (a = pattern.exec(str)) { str = str.replace(a[0], a[1]) } console.log(str); </script> </html>