Javascript字符串對象的常用方法簡明版
更新時間:2014年06月26日 13:16:57 投稿:junjie
這篇文章主要介紹了Javascript字符串對象的常用方法簡明版,本文用一個代碼例子列表了Javascript字符串對象的一些常用方法,例如加粗、斜體、刪除線、上標、下標、大小寫、查找字符、替換字符等,需要的朋友可以參考下
var a = "abcDEfGgdefg32asdf38";
document.write("原始:"+a+"<br />")
document.write("粗體:"+a.bold()+"<br />");
document.write("大號:"+a.big()+"<br />");
document.write("斜體:"+a.italics()+"<br />");
document.write("刪除線:"+a.strike()+"<br />");
document.write("字體大小:"+a.fontsize(10)+"<br />");
document.write("字體顏色:"+a.fontcolor("#ff0000")+"<br />");
document.write("上標:"+a.sup()+"<br />");
document.write("下標:"+a.sub()+"<br />");
document.write("大寫:"+a.toUpperCase()+"<br />");
document.write("大寫:"+a.toLowerCase()+"<br />");
document.write("返回索引字符:"+a.charAt(3)+"<br />");//這里應該是"D"
document.write("返回索引:"+a.indexOf("f")+"<br />");//這里應該是5
document.write("返回索引(逆向查找):"+a.lastIndexOf("f")+"<br />");//這里應該是9
document.write("查找字符串:"+a.search("f")+"<br />");//這里應該是5,與indexOf相同
document.write("替換字符串:"+a.replace("a","A")+"<br />");//把字符串中的a替換成A
document.write("返回子串:"+a.slice(1,3)+"<br />");//應該是bc,返回從索引1到3-1的子串
document.write("分割字符串:"+a.split("D").toString()+"<br />");//把D作為分割符,分割字符串,返回數組
document.write("返回子串:"+a.substr(1,2)+"<br />");//返回子串,從索引1開始,長度為2,這里就是bc
document.write("返回子串:"+a.substring(1,3)+"<br />");//與sclice()相同,返回索引1到3-1的子串
document.write("匹配:"+a.match(/\d+/)+"<br />");//正則匹配,返回匹配的結果,這里是32
相關文章
解析JavaScript面向對象概念中的Object類型與作用域
這篇文章主要介紹了解析JavaScript面向對象概念中的引用類型與作用域,文中重點講解了擴充函數運行作用域的需要的call和apply方法,朋友可以參考下2016-05-05

