chenhuiYj / note

开发笔记
6 stars 0 forks source link

练习题3:字符串乱码问题 #14

Open chenhuiYj opened 4 years ago

chenhuiYj commented 4 years ago

有乱码的字符串 'I? ?love? the?? ?great ? ? ???wall''I?love ??the ?great ??wall in ?beijing' 。需要讲字符串还原为正确的句子 "I Love the Great Wall""I Love The Great Wall in Beijing"

  1. 乱码字符只有 ' '?
  2. 如果单词前面是 ? 则该单词首字母大写

解题思路:先找出哪些单词前面有 ? 进行首字母大写处理。再把多余的 ''? 清楚

let str='I? ?love?  the?? ?great ? ? ???wall'
str.replace(/\?+([a-z])/g,function($1,$2,$3){return ' '+$2.toUpperCase()}).replace(/[\s\?]+/g,' ')
str='I?love ??the ?great ??wall in ?beijing'
str.replace(/\?+([a-z])/g,function($1,$2,$3){return ' '+$2.toUpperCase()}).replace(/[\s\?]+/g,' ')