您的位置:首页
运用正则+replace+substring将一段英语的字母大写 angurlar运用自定义指令filter完成首字母大写
2017-04-21 21:00
351 查看
复习下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应用了
<!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应用了
相关文章推荐
- 将一段英语的每个单词首字母大写
- 将一段英文每个单词首字母大写
- 请编写相关函数来完成对一段英文字符串进行字母统计功能,出现最多那个字母的出现次数,不区分大小写。
- python里使用capwords()函数来把字符里每一个英语单词首字母变大写
- 词组缩写 英语句子所有单词首字母大写输出 C语言/C++编程题
- JAVA--第七周实验--输出24个希腊字母与26个大写英文字母--运用类的继承
- JAVA--第七周实验--输出24个希腊字母与26个大写英文字母--运用类的继承
- 如何使用javascript将一段文字中的每一个单词的第一个字母转换为大写
- UITextField清除按钮的模式、关闭自动纠错单词、关闭自动首字母大写、数字键盘上加一个完成按钮
- Hdu 2026 首字母变大写
- hdoj2026 首字母变大写
- 详解Vue用自定义指令完成一个下拉菜单(select组件)
- tools - 首字母大写
- 【暑期基础2】A HDU 首字母变大写
- Android中spinner(下拉框)的使用1,和小写字母转换成大写
- 从一段字符串中快速找出某个字母或字符串出现的次数
- textField去掉默认首字母大写
- flash 与字符串:首字母大写
- 杭电ACM2026:首字母大写
- 关于自动完成功能的一段会话