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

阅读 42

2022-07-01

<!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>

 


精彩评论(0)

0 0 举报