正则表达式 把所有的花引号替换为直引号 把字符串中所有单词的首字母都转换为大写

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8" />
  <title>正则表达式 把所有的花引号替换为直引号 把字符串中所有单词的首字母都转换为大写</title>
</head>

<body>
  <div id="root"></div>
  <script>
  //把所有的花引号替换为直引号
  let name = '"a", "b"';
  let newName = name.replace(/"([^"]*)"/g, "'$1'");
  console.log(newName)  //'a', 'b'

  //把字符串中所有单词的首字母都转换为大写
  name = 'aaa bbb ccc';
  let uw = name.replace(/\b\w+\b/g, function(word) {
    return word.substring(0, 1).toUpperCase() + word.substring(1);
  });
  console.log(uw) //Aaa Bbb Ccc
  </script>
</body>

</html>

 

posted @ 2019-04-10 15:55  徐同保  阅读(316)  评论(0)    收藏  举报