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

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

猜你喜欢

转载自www.cnblogs.com/zxcv123/p/12041554.html