Regularization method -String distal base object

Regular Methods Chapter 7 String object

1, match method

stringObj.match(regex)

In the string matching substring search of the regex regular expression;

If a match, return an array containing the matching result; mismatch return null.

Regular expression in regex without a global modifier g, matched only once.

Regular expression regex in the global modifier with g, then matching all results.

	var str = "[email protected]";
	var res = str.match(/[@\.]/g); // .要加转义
	console.log(res); //["@", "."]

2, replace method

stringObj.replace(regex, replacement)

In the string matching substring search of the regex regular expression, and replace the specified replacement string;

Return new string after replacement.

The regex regular expression modifiers Without Global g, and replaces the previous one only match.

Regular expression with the regex global modifier g, all the matching result and replace all the results.

Replace when using the "$ 1" represents the first subexpression matches:

With $ 2 for the second sub-expression, and so on.

3, search method

stringObj.search(regex)

In line with the results of the search string in regular expression. If the result returned results found starting position, stopping the backward search , that is to say ignoring the global identifier G; if no matching results, -1 is returned.

	var str = "[email protected]";
	var res = str.search(/[@\.]/); // .要加转义
	console.log(res); //4

4, split method

stringObj.split(regex)

The string into a string array, returning an array

	var str = "[email protected]";
	var res = str.split(/[@\.]/);// .要加转义
	console.log(res); //["1234", "qq", "com"]
Released 1800 original articles · won praise 1922 · Views 170,000 +

Guess you like

Origin blog.csdn.net/weixin_42528266/article/details/105114683