js common string (3)

match()

match()The method takes a single argument, either a regular expression or an RegExpobject.

Calling this method essentially calling RegExpthe exec()same method,

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

//与 pattern.exec(text)相同
text.match(pattern); // ['cat', 'bat', 'sat', 'fat']

search()

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

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 返回字符串中第一个匹配项的索引

replace()

The first parameter is a string or a RegExp object (the string is not converted to a regular expression), the second parameter may be a string or a function, may be a special character sequence provided by some systems.

$$ $
$& 匹配整个模式的子字符串.与RegExp.lastMatch的值相同
$' 匹配的子字符串之前的子字符串.与RegExp.leftContext的值相同
$` 匹配的子字符串之后的子字符串.与RegExp.rightContext的值相同
$n 匹配第n个捕获组的子字符串,其中n等于0~9.
$nn 匹配第nn个捕获组的子字符串,其中nn等于01~99.

If the first argument is a string, it will replace the first substring; To replace all sub-strings, the only way is to provide a regular expression, but also to specify the global (g)flags.

var text = "cat, bat, sat, fat";
text.repalce("at", "ond");// "cond, bat, sat, fat"
text.replace(/at/g, "ond");// "cond, bond, sond, fond"

The second parameter replace () method if it is a function, in the case where only one match (i.e., the character string pattern matching), the three parameters passed to the function: pattern match, a match in the character pattern location string, the original string

split()

Based on the specified delimiter string into a plurality of sub-strings, and the results in an array, the separator can be a string, or may be a RegExp object (this method will not be as regular expression string type), the second optional parameter specifies the size of the array, return the array to ensure that does not exceed a predetermined size.

var colors = "red,blue,green,yellow";
var c1 = colorText.split(",");// ["red", "blue", "green", "yellow"]
var c2 = colorText.split(",", 2);// ["red", "blue"]
var c3 = colorText.split(/[^\,]+/);// ["", ",", ",", ",", ""]

localeCompare()

Compares two strings, and returns a value in the following:

  • If the string of the alphabet to be ahead of the string parameter, returns a negative, the majority of -1, the specific value depends on the implementation
  • If the string is equal to the string parameter, it returns 0
  • If the string of the alphabet to be behind a string parameter, it returns a positive number, return a majority of the specific value depends on the implementation
var stringValue = "yellow";
stringValue.localeCompare("brick");// 1
stringValue.localeCompare("yellow");// 0
stringValue.localeCompare("zoo"); -1

fromCharCode()

This method is static String constructor itself

String.fromCharCode(104, 101, 108, 108, 111);// hello

Guess you like

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