js中常见字符串类型操作方法(2)

我与影子孤独终老i 提交于 2019-12-15 01:25:20

toLowerCase(),toLocalLowerCase(),toUpperCase(),toLocaleUpperCase()

var stringValue = "hello world";
stringValue.toLowerCase();// "hello world"
stringValue.toUpperCase();// "HELLO WORLD"

stringValue.toLocaleLowerCase();// "hello world"
stringValue.toLocaleUpperCase();// "HELLO WORLD"

一般来说,在不知道自己的代码将在哪种语言环境中运行的情况下,还是使用针对地区的方法更稳妥一些。

match()

match()方法只接受一个参数,要么是一个正则表达式,要么是一个 RegExp 对象。

var text = "cat, bat, sat, fat"; 
var pattern = /.at/; 

//与 pattern.exec(text)相同
var matches = text.match(pattern); 
console.log(matches.index); //0 
console.log(matches[0]); //"cat" 
console.log(pattern.lastIndex); //0

search()

这个方法的唯一参数与 match()方法的参数相同:由字符串或 RegExp 对象指定的一个正则表达式。

返回字符串中第一个匹配项的索引;如果没有找到匹配项,则返回-1

var text = "cat, bat, sat, fat";
var pos = text.search(/at/);
console.log(pos);// 1
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!