格式化货币(只转换小数点前的整数部分)实现效果,遵循的原则:==>123,456,789123456789.1234==>123,456,789.123constformatMoney=(money)=>{returnmoney.replace(newRegExp(`(?!^)(?=(\\d{3})+${money.includes('.')?'\\.':'$'})`,'g'),','))}formatMoney('123456789')//'123,456,789'formatMoney('123456789.123')//'123,456,789.123'formatMoney('123')//'123'formatMoney('123456789.123456')//'127,4951字符串首末空格方法一:consttrim1=(str)=>{returnstr.replace(/^\s*|s*$/g,'')}conststring='helloworld'constnoSpaceString='helloworld'consttrimString=trim1(string)console.log(string)//helloworldconsole.log(trimString,trimString===noSpaceString)//helloworld,trueconsole.log(string)//helloworld方法二:consttrim2=(str)=>{returnstr.replace(/^\s*(.*?)\s*$/g,'$1')}conststring='hellomedium'constnoSpaceString='你好媒体'常量修剪字符串=trim2(string)console.log(string)//hellomediumconsole.log(trimString,trimString===noSpaceString)//hellomedium,trueconsole.log(string)//hellomedium解析链接上的搜索参数//urlhttps://qianlongo.github.io/vue-demos/dist/index.html?name=fatfish&age=100#/homeconstgetQueryByName=(name)=>{constqueryNameRegex=newRegExp(`[?&]${name}=([^&]*)(&|$)`)constqueryNameMatch=window.location.search.match(queryNameRegex)返回queryNameMatch?decodeURIComponent(queryNameMatch[1]):''}constname=getQueryByName('name')constage=getQueryByName('age')console.log(name,age)//fatfish,100驼峰式字符符号示例:fooBar=>fooBarfoo-bar----=>fooBarfoo_bar__=>fooBarconstcamelCase=(string)=>{constcamelCaseRegex=/[-_\s]+(.)?/greturnstring.replace(camelCaseRegex,(匹配,char)=>{returnchar?char.toUpperCase():''})}console.log(camelCase('fooBar'))//fooBarconsole.log(camelCase('foo-bar--'))//fooBarconsole.log(camelCase('foo_bar__'))//fooBar将字符串的首字母转换为大写PleaseconverthelloworldtoHelloWorld.constcapitalize=(string)=>{constcapitalizeRegex=/(?:^|\s+)\w/greturnstring.toLowerCase().replace(capitalizeRegex,(match)=>match.toUpperCase())}控制台。log(capitalize('helloworld'))//HelloWorldconsole.log(capitalize('helloWORLD'))//HelloWorld以3-4-4格式划分电话号码letmobile='18379836654'letmobileReg=/(?=(\d{4})+$)/gconsole.log(mobile.replace(mobileReg,'-'))//183-7983-6654
