js string type common method of operation (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"

In general, without knowing their code will run in which language environment, the method or use the region for some of the more robust.

match()

match () method takes a single argument, either a regular expression, or is a RegExp object.

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()

The only parameter of this method the same parameters and match () method: specified by string or a regular expression RegExp object.

Returns a string matching the first index entry; if no match is found, returns -1

var text = "cat, bat, sat, fat";
var pos = text.search(/at/);
console.log(pos);// 1

Guess you like

Origin www.cnblogs.com/zxcv123/p/12041554.html