javascript 学习笔记——String

本文详细介绍了JavaScript中的String操作、Number与String的转换方法及正则表达式的使用技巧。通过实例演示了字符串的创建、属性和方法,如length、charAt等,并展示了如何利用正则表达式进行文本匹配。
[size=medium]记录以下网址的javascript core crash章节[url]http://courses.coreservlets.com/Course-Materials/ajax-basics.html[/url][/size]

[b][size=x-large]1、String常用[/size][/b]
[size=medium]下面说有错的地方其实没错,因为二进制里面不存在2这个字符啊,只有0和1!!![/size]
[align=center][img]http://dl.iteye.com/upload/attachment/0061/9792/0974b4b5-d92e-3748-abf3-378776d60f60.png[/img][/align]
[b][size=x-large]2、String、Number互相转换[/size][/b]

[align=center][img]http://dl.iteye.com/upload/attachment/0061/9794/80c3bf96-53ca-3a3f-b20e-02a8c2140daa.png[/img][/align]

[b][size=x-large]3、正则表达式[/size][/b]
[size=large]正则表达式中的特殊字符(同组内以逗号隔开):[/size]
[quote]
– ^, $, . – 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 n n 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
[/quote]

[size=large]正则表达式的选项:[/size]
[quote]
– /pattern/g – do global matching (find all matches, not just first one)
– /pattern/i – do case-insensitive matching
– /pattern/m – do multiline matching
[/quote]

[align=center][img]http://dl.iteye.com/upload/attachment/0061/9796/6ca986bd-539d-3eed-bd06-68c934b316ee.png[/img][/align]

[size=large]本文源代码:[/size]

<!-- 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[i]);
//}
document.write("<p>'testABCDABCabc'.split(/A/i)="
+ 'testABCDABCabc'.split(/A/i) + "\t加个选项i,就变成大小写不敏感了");
</script>

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值