热门
const text = 'snake_case_string'; const camelCaseText = text.replace(/_([a-z])/g, (match, letter) => letter.toUpperCase()); // 'snakeCaseString'
const sentence = 'This is a sample sentence'; const reversedWords = sentence.split(' ').reverse().join(' '); // 'sentence sample a is This'
const text = 'this is a title'; const titleCaseText = text.replace(/\b\w/g, (match) => match.toUpperCase()); // 'This Is A Title'
const htmlText = '<p>This is <b>bold</b> text.</p>'; const strippedText = htmlText.replace(/<[^>]*>/g, ''); // 'This is bold text.'
const text = 'Hello, World!'; const base64Encoded = btoa(text); // 'SGVsbG8sIFdvcmxkIQ=='
作用: 使用扩展运算符将字符串转换为字符数组。
示例:
const text = 'JavaScript'; const charArray = [...text]; // ['J', 'a', 'v', 'a', 'S', 'c', 'r', 'i', 'p', 't']
常见场景: 以更灵活的方式处理字符串中的字符。