JS字符串使用占位符轻松实现拼接(来自react源码)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xutongbao/article/details/89002174
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width,height=device-height">
  <title>JS字符串使用占位符轻松实现拼接(来自react源码)</title>
  <style>
  </style>
</head>

<body>
  <script>
  let args = ['xu', '徐']
  let s = '%s , %s'
  let argIndex = 0
  let newString = '%s , %s'.replace(/%s/g, () => args[argIndex++])
  console.log(s) //%s , %s
  console.log(newString) //xu , 徐
  </script>
</body>

</html>

猜你喜欢

转载自blog.csdn.net/xutongbao/article/details/89002174