Skip to content
欢迎扫码关注公众号

JavaScript 中 format 字符串方法

javascript
function format(val) {
  var args = Array.prototype.slice.call(arguments, 1)
  return val.replace(/{(\d+)}/g, function(match, number) {
    return typeof args[number] !== 'undefined' ? args[number] : match
  })
}

export default {
  format: format
}
javascript
import { format } from '@/utils/string.js'

format('Hello, {0}.', 'JiaJia')

另外,在 ES6 中可以使用字符串插值:

javascript
let soMany = 10;
console.log(`This is ${soMany} times easier!`);

方法引用自 StackOverflow


Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.