String的一些基本操作(startsWith、split)和JS中的match、search和replace方法
来源:互联网 发布:怎么在手机淘宝买东西 编辑:程序博客网 时间:2024/06/05 22:56
<span style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; line-height: 17.6000003814697px;"><strong></strong></span><pre name="code" class="java"><span style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; line-height: 17.6000003814697px;">1.JS中的match、search和replace方法</span>
</pre><pre name="code" class="javascript" style="font-size: 16px;"><span style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; line-height: 17.6000003814697px;">/* search方法</span>
* str.search(regexp) * 查找字符串str中是否有匹配regexp的子串,如果匹配则返回匹配子串的索引。否则返回-1。 */"hello123".search(/\d+/); // 5"hello123".search(/\d{4}/); // -1 /* match方法 * str.match(regexp) * 直接看例子,分别带g标识和不带g标识 */str = "For more information, see Chapter 3.4.5.1 chapter 4.5";re = /(chapter \d+(\.\d)*)/i; found = str.match(re); // 输出为["Chapter 3.4.5.1", "Chapter 3.4.5.1", ".1"],第二、第三个元素为捕获 str = "For more information, see Chapter 3.4.5.1 chapter 4.5";re = /(chapter \d+(\.\d)*)/gi; found = str.match(re); // 输出为["Chapter 3.4.5.1", "chapter 4.5"]// 最基础使用var name = "hey duke";name = name.replace("k", "d");console.log(name); // "hey dude" // 稍高级用法(正则模式替换)var alphaStr = "hello123".replace(/\d+/gi, "");console.log(alphaStr); // "hello"// 带反向引用var alphaSt2 = "Smith John".replace(/(\w+)\s*(\w+)/gi, "$2, $1");console.log(alphaStr2); // "John, Smith" // 较高级用法:第二个参数设为function,修改匹配子串var hadoop = "Had".replace("d", function(match) { return match+"doop";});console.log(hadoop); // "Hadoop" "yyyXXzzzz".replace(/(X*)(z{3})/, function (str, p1, p2, offset, s) { console.log(offset); console.log(s); return str + " - " + p1 + " , " + p2; } );/*其中str是匹配子字符串,p1、p2是捕获,offset是匹配发生位置索引,s是源字符串,这里是"yyyXXzzzz"输出为:3yyyXXzzzz"yyyXXzzz - XX , zzzz"*/
2.String的startWith方法的使用!
今天发现了一个startsWith()方法的重载,startsWith(String str,int Index),当时上网去查了一下没有后边这个方法,于是自己做了一下测试,现在给出如下详细解释,startsWith(String str)就是检查形参参数是否与你要检查的字符串开头相同,而startsWith(Sring str,int Index)则是从你要比较的原字符串的指定下标开始和形参作比较,这里边呢str.startsWith(String str)和str.startsWith(string str,0)是没有区别的,下边就看代码吧!
String st1 = "dsadas"; System.out.println("st1:"+st1.startsWith("sa",1)); String st2 = "dsadas"; System.out.println("st1:"+st2.startsWith("sa")); String st3 = "dsadas"; System.out.println("st2:"+st3.startsWith("ds")); String st4 = "dsadas"; System.out.println("st4:"+st4.startsWith("ds",0)); String st5 = "dsadas"; System.out.println("st5:"+st5.startsWith("ds",1));输出结果为:st1:truest1:falsest2:truest4:truest5:false
3.String的split(String regex,int limit)方法小结
split(String regex, int limit)方法,头一个参数String regex表示字符串分割的模式,包括分隔符和正则表达式;但是第二个参数limit比较迷糊人,api中这样解释:limit 参数控制模式应用的次数,因此影响所得数组的长度。如果该限制 n 大于 0,则模式将被最多应用 n - 1 次,数组的长度将不会大于 n,而且数组的最后一项将包含所有超出最后匹配的定界符的输入。如果 n 为非正,那么模式将被应用尽可能多的次数,而且数组可以是任何长度。如果 n 为 0,那么模式将被应用尽可能多的次数,数组可以是任何长度,并且结尾空字符串将被丢弃。
0 0
- String的一些基本操作(startsWith、split)和JS中的match、search和replace方法
- JavaScript String对象方法——search(),match(),replace(),split()
- JS的正则表达式常用方法test exec match search replace split
- match、replace、search、split
- javascript-string类型的方法(match,replace,split,concat+join)
- javascript字符串string常用的几种方法(indexOf/lastIndexOf/search/charAt/match/slice/substr/replace/concat/split
- js正则表达式search() 搜索、replace()替换、match()方法exec()方法test() 方法split() 方法
- 正则方法exec和test,String方法match,replace
- javascript 正则表达式数量词,字符串方法(match,search,replace,split),贪婪模式,惰性模式总结
- Android关于String数据的startsWith()和endsWith()函数操作
- js中的indexOf以及startsWith和endsWith方法
- c++使用 STL string 实现split,trim和replace方法
- Java中的String的replace方法和replaceAll方法
- test(),compile(),exec(),match(),search(),replace(),split()
- JavaScript: 常用的match 和search方法
- js正则函数match、exec、test、search、replace、split使用介绍集合
- js正则函数match、exec、test、search、replace、split使用介绍集合
- js正则函数match、exec、test、search、replace、split使用介绍集合
- 本机访问虚拟机web服务器失败的解决办法
- MultiByteToWideChar和WideCharToMultiByte用法详解
- 2015山东省赛总结
- DP计数
- iOS开发之关于cell的重用机制
- String的一些基本操作(startsWith、split)和JS中的match、search和replace方法
- Jmeter测试Http接口 Jmeter2.13r
- android postInvalidate() callback onDraw Invalid
- 第五章类和对象2015-05-12
- sql语句 查询或clob的文本内容
- 从今天开始记录!
- 关于Fragment实现tab标签效果的提示
- u-boot 启动过程 (基于S3C2410) 和uImage zImage 启动分析
- 游戏外挂基本原理及实现