免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1573 | 回复: 1
打印 上一主题 下一主题

javascript 学习笔记——String [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-11 15:38 |只看该作者 |倒序浏览
javascript 学习笔记——String





记录以下网址的javascript core crash章节http://courses.coreservlets.com/Course-Materials/ajax-basics.html

1、String常用




2、String、Number互相转换





3、正则表达式
正则表达式中的特殊字符(同组内以逗号隔开):

引用
  1. –  ^, $, .        – beginning, end of string, any one char ,
  2. –  \ – escape what would otherwise be a special character
  3. –  *, +, ?       – 0 or more, 1 or more, 0 or 1 occurrences
  4. –  {n}  {n }   – exactly n  n or more occurrences {n}, {n,}      exactly n, n or more occurrences
  5. –  []               – grouping
  6. –  \s, \S          – whitespace, non-whitespace
  7. \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代码
  1. <!-- LCTestJS_String.html version: 2012_01_11 -->  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html>  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  6. <title>测试javascript 的string version: 2012_01_11</title>  
  7. <style>  
  8. h3 {   
  9.     color: #FFF;   
  10.     background-color: #09F;   
  11.     font-style: normal;   
  12.     font-weight: bold;   
  13. }   
  14.   
  15. h4 {   
  16.     font-weight: bolder;   
  17.     color: #d00;   
  18.     background-color: #CCC;   
  19. }   
  20. </style>  
  21.   
  22. </head>  
  23. <body>  
  24.   
  25.     <h2>测试javascript 的string version: 2012_01_11</h2>  
  26.   
  27.     <h3>String——常用的</h3>  
  28.     <h4>  
  29.         单引号双引号均可   
  30.         <p>用==判断两个字符串内容是否相同</p>  
  31.     </h4>  
  32.     <script type="text/javascript">  
  33.         var strDoubleQuotes = "asdf";   
  34.         var strSingleQuote = 'asdf';   
  35.         var isEqual = strDoubleQuotes == strSingleQuote;   
  36.         document.write("<p>\"asdf\"=='asdf'?\t" + isEqual);   
  37.     </script>  
  38.   
  39.     <h4>.length属性:字符串长度</h4>  
  40.     <script type="text/javascript">  
  41.         document.write("<p>'asdf'.length=\t" + 'asdf'.length);   
  42.     </script>  
  43.   
  44.   
  45.   
  46.     <h4>常用函数:类似java</h4>  
  47.     charAt, indexOf, lastIndexOf, substring, toLowerCase, toUpperCase   
  48.   
  49.     <h4>html专用函数(非标准)</h4>  
  50.     <script type="text/javascript">  
  51.         'test'.bold().italics().fontcolor('red')   
  52.         document.write("<p>'test'.bold().italics().fontcolor('red')="   
  53.                 + 'test'.bold().italics().fontcolor('red'));   
  54.     </script>  
  55.   
  56.     <h3>String,Number互相转换</h3>  
  57.     <h4>  
  58.         Number转String: .toFixed(num):按num保留小数点后的位数(四舍五入)   
  59.         <p>可以通过new String(arg)来明确声明为String类型(其他类型类似)</p>  
  60.     </h4>  
  61.     <script type="text/javascript">  
  62.         document.write("<p>(1.236).toFixed(2)=\t" + (1.236).toFixed(2));   
  63.         var n = 1.236;   
  64.         document.write("<p>(1.236).length=\t" + n.length);   
  65.         document.write("<p>(1.236).toFixed(2).length=\t"   
  66.                 + (1.236).toFixed(2).length);   
  67.         document.write("<p>new String(1.236).length=\t"   
  68.                 + new String(1.236).length);   
  69.     </script>  
  70.   
  71.     <h4>String转Number;从第一个字母开始被忽略</h4>  
  72.     <script type="text/javascript">  
  73.         var s = new String('1234ff');   
  74.         document.write("<p>parseInt('12.34ff')=" + parseInt('12.34ff'));   
  75.         document.write("<p>parseInt('12.34ff', 2)=" + parseInt('12.34ff', 2)   
  76.                 + "\t错了吧?");   
  77.         document.write("<p>parseInt('12.34ff', 3)=" + parseInt('12.34ff', 3));   
  78.         document.write("<p>parseInt('12.34ff', 4)=" + parseInt('12.34ff', 4));   
  79.         document.write("<p>parseInt('12.34ff', 5)=" + parseInt('12.34ff', 5));   
  80.         document.write("<p>----");   
  81.         document.write("<p>parseFloat('12.34ff')=" + parseFloat('12.34ff'));   
  82.     </script>  
  83.   
  84.     <h3>正则表达式</h3>  
  85.     <h4>  
  86.         正则表达式: 用在两个斜杠(/表达式/)中间的式子,而非String来表示   
  87.         <p>可用于函数:match, replace, search, split</p>  
  88.     </h4>  
  89.     <script type="text/javascript">  
  90.         document.write("<p>'testABCDABCabc'.split(/A/)="   
  91.                 + 'testABCDABCabc'.split(/A/)   
  92.                 + "\t结果是数组,直接输出以逗号隔开,可以自己用for语句输出");   
  93.         //var splited='testABCDABCabc'.split(/A/);   
  94.         //for(var i=0;i<splited.length;i++){   
  95.         //  document.write("<p>"+splited[i]);   
  96.         //}   
  97.         document.write("<p>'testABCDABCabc'.split(/A/i)="   
  98.                 + 'testABCDABCabc'.split(/A/i) + "\t加个选项i,就变成大小写不敏感了");   
  99.     </script>  
  100.   
  101. </body>  
  102. </html>  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-01-12 10:34 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP