修饰符:
i 执行对大小写不敏感的匹配。
g 执行全局匹配(查找所有匹配而非在找到第一个匹配后停止)。
m 执行多行匹配。
一、javascript字符串提供的功能:
var str = 'http://localhost:8089/dist/index.html#/set_product';
1、match(regexp: string | RegExp): RegExpMatchArray | null;
/**
* Matches a string with a regular expression, and returns an array containing the results of that search.
* @param regexp A variable name or string literal containing the regular expression pattern and flags.
*/
str.match('/^http/i');
var e = str.match('/^https/i') ? "https://" : "http://";
2、search(regexp: string | RegExp): number
/**
* Finds the first substring match in a regular expression search.
* @param regexp The regular expression pattern and applicable flags.
*/
用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串,并返回子串的起始位置。
str.search('/^http/i');
如果没有匹配到则返回 -1
3、replace(searchValue: string | RegExp, replaceValue: string): string;
/**
* Replaces text in a string, using a regular expression or search string.
* @param searchValue A string to search for.
* @param replaceValue A string containing the text to replace for every successful match of searchValue in this string.
*/
用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
str.replace('/login_page', '/login');
二、使用 RegExp 对象
exec 方法
/**
* Executes a search on a string using a regular expression pattern, and returns an array containing the results of that search.
* @param string The String object or string literal on which to perform the search.
*/
exec(string: string): RegExpExecArray | null;
test 方法
/**
* Returns a Boolean value that indicates whether or not a pattern exists in a searched string.
* @param string String on which to perform the search.
*/
test(string: string): boolean;
查找路由,期望得到的结果 /set_product
var patt = /index.html#(.*)/;
var mat = patt.exec(str);
str.replace(mat[1], '/login');