js search match indexof
来源:互联网 发布:淘宝导航栏怎么做 编辑:程序博客网 时间:2024/05/15 07:00
String 的search方法和match方法
var str="Define a class with a Class Definition.";
var searchValue=/[A-Za-z]lass/g;
//搜索第一个可以匹配的位置,该语句返回9
document.write(str.search(searchValue),"<br>");
//返回所有匹配的子字符串,该语句返回Class和class
document.write(str.match(searchValue),"<br>");
<title>返回一个字符串在另一个字符串中出现的次数</title>
<script type="text/javascript">
var str="Define a class with a class Definition.";
var searchValue="class";
//定义出现次数的变量
var searchCount=0;
//查看子字符串第一次在字符串中出现的位置
var fromIndex =str.indexOf(searchValue);
while(fromIndex!=-1){
searchCount++;
//再次进行查询,查询的开始位置为子字符串出现的位置加上子字符串的长度
fromIndex=str.indexOf(searchValue,fromIndex+searchValue.length);
}
document.write("“",searchValue,"”在字符串",str,"“中出现的次数为:",searchCount,"次<br>");
</script>
var oldstr="Define a class with a Class Definition.";
document.write(oldstr,"<br>");
var newStr=oldstr.replace("class","abc");
document.write(newStr,"<br>");
//使用正则表达式匹配药替换的字符串
var searchValue=/class/gi;
var newstr2=oldstr.replace(searchValue,'abc');
document.write(newstr2,"<br><br>");
//更多的替换字符串
//替换函数
function replaceStr(str){
//将参数转换成大写并返回
return str.toUpperCase();
}
var newstr3=oldstr.replace("class",replaceStr("abc"));
document.write(newstr3,"<br>");
- js search match indexof
- js 中的window.location.search.match("XX")
- js正则exec,match,search用法练习
- indexOf()、match()、replace()方法
- 字符串字符JS:1.8,字符串(string)对象(length,indexOf(),lastIndexOf(),replace(),match(),toUpperCase(),toLowerCas
- JS:1.8字符串(string)对象(length,indexOf(),lastIndexOf(),replace(),match(),toUpperCase(),toLowerCase())
- javascript字符串string常用的几种方法(indexOf/lastIndexOf/search/charAt/match/slice/substr/replace/concat/split
- Js 正则表达式对象(方法:exec、test、match、search)
- js正则函数match、search、exec、test使用介绍集合
- indexOf与search的区别
- Custom Search Match Fields
- string search and match
- search vs. match
- match、replace、search、split
- js indexOf("") toUpperCase()
- js中indexof详解
- js indexof用法
- js中的indexOf
- Singleton Pattern
- 简单工厂模式和工厂方法模式
- 短信的烦扰
- openssl - SSL通讯之程序结构
- XSL
- js search match indexof
- ubuntu 10.10 屏幕闪的解决方法
- 《.NET 4.0面向对象编程漫谈》勘误表(2011年1月14日更新)
- 无题
- Java生成随机字符串的代码
- START WITH...CONNECT BY 学习!
- ASP.NET 3.5路由总结篇
- phpmyadmin登录问题和require出现乱码
- Chapter 6 -- SystemC From Gound 读书心得