运用正则+replace+substring将一段英语的字母大写 angurlar运用自定义指令filter完成首字母大写
复习下js基础并运用正则+replace+substring将一段英语的字母大写
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首字母大写</title>
</head>
<body>
<div class="dv"></div>
</body>
</html>
<script>
var str = "this is a javascript string";
str = str.replace(/\b\w+\b/g,function(word){ //运用正则方式将语句截取成一个一个的单词
console.log(word);//打印出来截取的字符串
console.log(word.substring(0,1).toUpperCase())//将截取的首字母大写
console.log(word.substring(0,1).toUpperCase()+word.substring(1)) //将大写的字符串拼接到未大写的上面
return word.substring(0,1).toUpperCase()+word.substring(1);//将所有的字符return出去
})
document.write(str)//在页面上打印出来
</script>
配合angularjs 的自定义指令将命令输出
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<style>
input.ng-invalid{
background: red;
}
</style>
<body>
<div ng-app="myApp" ng-init='firstName="Jon"' ng-controller="mycrl">
<input type="text" ng-model="name" required/>
<h2>年龄:{{age}}</h2>
{{ str | toUpercase}} //输出结果
</div>
</body>
</html>
<script src="js/angurlar素材/angular/angular.js"></script>
<script>
var app =angular.module("myApp",[]);
app.controller("mycrl",function($scope){
$scope.name="";
$scope.age =18;
$scope.str= "this is my first work"
});
app.filter("toUpercase",function(){//自定义指令
return function(str){
return str.replace(/\b\w+\b/g,function(word){//replace配合正则截取每一个单词
return word.substring(0,1).toUpperCase()+word.substring(1);//运用substring截取每个单词的首字母并大写
});
}
})
</script>
这样就输出个简单的angular应用了
运用正则+replace+substring将一段英语的字母大写 angurlar运用自定义指令filter完成首字母大写的更多相关文章
- java String中的replace(oldChar,newChar) replace(CharSequence target,CharSequence replacement) replaceAll replaceFirst 面试题:输入英文语句,单词首字符大写后输出 char String int 相互转换
package com.swift; import java.util.Scanner; public class FirstChat_ToCaps_Test { public static void ...
- JS replace()方法-字符串首字母大写
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. replace()方法有两个参数,第一个参数是正则表达式,正则表达式如果带全局标志/g,则是代表替换 ...
- 正则replace 回调函数里接收的参数是什么?
前言 我们都知道 replace 在做替换处理方面会很常用,通常也是第一个会想到的方法.replace 第一个参数可以传入 string 或 RegExp,第二个参数可以传入 string 或 一个回 ...
- java 正则 replace 忽略大小写
String description = model.getDescription(); if (!"".equals(description)) { //replace(/\&l ...
- C#正则实现匹配一块代码段
最近项目,生成聚合网关,但是生成的网关文件中,存在着不必要的代码段,比如一个类A,类B等 之前一直使用手动删除,这么做劳民伤财,浪费时间,考虑使用正则写一个工具实现自动删除. 正则写法: string ...
- Js正则Replace方法
JS正则的创建有两种方式: new RegExp() 和 直接字面量. //使用RegExp对象创建 var regObj = new RegExp("(^\s+)|(\s+$)" ...
- JS正则 replace()方法全局替换变量(可以对变量进行全文替换)
转至:https://www.cnblogs.com/jasonlam/p/7070604.html var text = "饿~,23333.饿~,测试yongde"; var ...
- JS学习笔记 - fgm练习 - 输入数字求和 正则replace onkeyup事件
<style> body{font-size: 12px;} .outer{ width: 500px; margin: 0 auto; } span{ color: #999; } in ...
- mysql replace substring 字符串截取处理
SELECT a1,a2,replace(a2, "豫ICP备16006180号-", "") a22,a3,a4,a5 FROM `aaab` order b ...
随机推荐
- [bzoj1497][NOI2006]最大获利_网络流_最小割
最大获利 bzoj-1497 题目大意:可以建立一个点,花费一定的代价:将已经建立的两个点之间连边,得到一定收益.有些节点之间是不允许连边的. 注释:1<=点数<=5,000,1<= ...
- [poj2923]Relocation_状压dp_01背包
Relocation poj-2923 题目大意:给出n个物品,有两辆车,两辆车必须一起出动并且每辆车有单独的容量.问最少需要运输多少次才能运走所有货物. 注释:n<=10,容量,物品代价< ...
- python爬虫---抓取优酷的电影
最近在学习爬虫,用的BeautifulSoup4这个库,设想是把优酷上面的电影的名字及链接爬到,然后存到一个文本文档中.比较简单的需求,第一次写爬虫.贴上代码供参考: # coding:utf-8 i ...
- JVM活学活用——优化springboot
介绍 在SpringBoot的Web项目中,默认采用的是内置Tomcat,当然也可以配置支持内置的jetty,内置有什么好处呢? 1. 方便微服务部署. 2. 方便项目启动,不需要下载Tomcat或者 ...
- Windows 安装nginx并开机启动
Win安装nginx并 开机启动 下载nginx安装包 nginx-1.12.2.zip,解压到D盘. https://pan.baidu.com/s/1InQa527yq35Q68c73RBb-A# ...
- 杭电OJ2004——成绩转换
/*成绩转换Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 每日冲刺报告——Day3(Java-Team)
第三天报告(11.4 周六) 团队:Java-Team 成员: 章辉宇(284) 吴政楠(286) 陈阳(PM:288) 韩华颂(142) 胡志权(143) github地址:https://git ...
- Linux下ip配置与网络重启
ip配置 //以下ip配置重启失效 sudo ifconfig 192.168.1.1 sudo ifconfig 192.168.1.1 netmask 255.255.255.0 网络重启 //关 ...
- Flask Session 详解
会话session ,允许你在不同请求 之间储存信息.这个对象相当于用密钥签名加密的 cookie ,即用户可以查看你的 cookie ,但是如果没有密钥就无法修改它. from flask impo ...
- wpf研究之道——datagrid控件数据绑定
前台: <DataGrid x:Name="TestCaseDataGrid" ItemsSource="{Binding}" > {binding ...