javascript 学习笔记——String
javascript 学习笔记——String记录以下网址的javascript core crash章节http://courses.coreservlets.com/Course-Materials/ajax-basics.html
1、String常用
2、String、Number互相转换
3、正则表达式
正则表达式中的特殊字符(同组内以逗号隔开):
引用–^, $, . – beginning, end of string, any one char ,
–\ – escape what would otherwise be a special character
–*, +, ? – 0 or more, 1 or more, 0 or 1 occurrences
–{n}{n } – exactly nn or more occurrences {n}, {n,} exactly n, n or more occurrences
–[] – grouping
–\s, \S – whitespace, non-whitespace
\w\W word char (letter or number)non word char –\w, \W – word char (letter or number), non-word char 正则表达式的选项:
引用
–/pattern/g– do global matching (find all matches, not just first one)
–/pattern/i – do case-insensitive matching
–/pattern/m – do multiline matching
本文源代码:
Html代码<!-- LCTestJS_String.html version: 2012_01_11 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试javascript 的string version: 2012_01_11</title>
<style>
h3 {
color: #FFF;
background-color: #09F;
font-style: normal;
font-weight: bold;
}
h4 {
font-weight: bolder;
color: #d00;
background-color: #CCC;
}
</style>
</head>
<body>
<h2>测试javascript 的string version: 2012_01_11</h2>
<h3>String——常用的</h3>
<h4>
单引号双引号均可
<p>用==判断两个字符串内容是否相同</p>
</h4>
<script type="text/javascript">
var strDoubleQuotes = "asdf";
var strSingleQuote = 'asdf';
var isEqual = strDoubleQuotes == strSingleQuote;
document.write("<p>\"asdf\"=='asdf'?\t" + isEqual);
</script>
<h4>.length属性:字符串长度</h4>
<script type="text/javascript">
document.write("<p>'asdf'.length=\t" + 'asdf'.length);
</script>
<h4>常用函数:类似java</h4>
charAt, indexOf, lastIndexOf, substring, toLowerCase, toUpperCase
<h4>html专用函数(非标准)</h4>
<script type="text/javascript">
'test'.bold().italics().fontcolor('red')
document.write("<p>'test'.bold().italics().fontcolor('red')="
+ 'test'.bold().italics().fontcolor('red'));
</script>
<h3>String,Number互相转换</h3>
<h4>
Number转String: .toFixed(num):按num保留小数点后的位数(四舍五入)
<p>可以通过new String(arg)来明确声明为String类型(其他类型类似)</p>
</h4>
<script type="text/javascript">
document.write("<p>(1.236).toFixed(2)=\t" + (1.236).toFixed(2));
var n = 1.236;
document.write("<p>(1.236).length=\t" + n.length);
document.write("<p>(1.236).toFixed(2).length=\t"
+ (1.236).toFixed(2).length);
document.write("<p>new String(1.236).length=\t"
+ new String(1.236).length);
</script>
<h4>String转Number;从第一个字母开始被忽略</h4>
<script type="text/javascript">
var s = new String('1234ff');
document.write("<p>parseInt('12.34ff')=" + parseInt('12.34ff'));
document.write("<p>parseInt('12.34ff', 2)=" + parseInt('12.34ff', 2)
+ "\t错了吧?");
document.write("<p>parseInt('12.34ff', 3)=" + parseInt('12.34ff', 3));
document.write("<p>parseInt('12.34ff', 4)=" + parseInt('12.34ff', 4));
document.write("<p>parseInt('12.34ff', 5)=" + parseInt('12.34ff', 5));
document.write("<p>----");
document.write("<p>parseFloat('12.34ff')=" + parseFloat('12.34ff'));
</script>
<h3>正则表达式</h3>
<h4>
正则表达式: 用在两个斜杠(/表达式/)中间的式子,而非String来表示
<p>可用于函数:match, replace, search, split</p>
</h4>
<script type="text/javascript">
document.write("<p>'testABCDABCabc'.split(/A/)="
+ 'testABCDABCabc'.split(/A/)
+ "\t结果是数组,直接输出以逗号隔开,可以自己用for语句输出");
//var splited='testABCDABCabc'.split(/A/);
//for(var i=0;i<splited.length;i++){
//document.write("<p>"+splited);
//}
document.write("<p>'testABCDABCabc'.split(/A/i)="
+ 'testABCDABCabc'.split(/A/i) + "\t加个选项i,就变成大小写不敏感了");
</script>
</body>
</html> 谢谢分享
页:
[1]