In the actual front-end development, we use replace for string substitution, such as:
var str = ' 1231 ';
Str.replace (' 1 ', ' a '); ' a231 '
If you want to replace all of the characters in the string, you can use a regular:
var str = ' 1231 ';
Str.replace (/1/g, ' a '); ' a23a '
But if we want to replace the variable, we can't do it with the above method:
var str = ' 1231 ';
var c = ' 1 ';
Str.replace (/c/g, ' a '); ' 1231 '
The variable C in the regular is the default into the string ' C ', does not achieve the desired effect, in this case, we need to use the regular method of regexp;
var str = ' 1231 ';
var c = ' 1 ';
var reg = new RegExp (c, ' G ');
Str.replace (Reg, ' a '); ' a23a '
This is a general method of replacing variable substitution.
The following example is used to get two parameters of the URL and return the real URL before urlrewrite
var reg=new RegExp ("(http://www.qidian.com/BookReader/) (\\d+), (\\d+). aspx", "GMI");
var url= "http://www.qidian.com/BookReader/1017141,20361055.aspx";
Way one, the simplest common way
var rep=url.replace (Reg, "$1showbook.aspx?bookid=$2&chapterid=$3");
Alert (rep);
Method two, using the callback function of the fixed parameter
var rep2=url.replace (reg,function (M,P1,P2,P3) {return p1+ "showbook.aspx?bookid=" +p3+ "&chapterid=" +p3});
alert (REP2);
Method III, using a callback function with a non fixed parameter
var rep3=url.replace (Reg,function () {var args=arguments; return args[1]+ showbook.aspx?bookid= "+args[2]+" & Chapterid= "+args[3];}";
alert (REP3);
Method Four
Mode four and method three are very similar, in addition to returning the replacement string, you can also get the parameters alone
var BookID;
var Chapterid;
function Captext ()
{
var args=arguments;
BOOKID=ARGS[2];
CHAPTERID=ARGS[3];
return args[1]+ "showbook.aspx?bookid=" +args[2]+ "&chapterid=" +args[3];
}
var rep4=url.replace (Reg,captext);
alert (REP4);
alert (BookID);
alert (Chapterid);
To get a group by using the test method
var reg3=new RegExp ("(http://www.qidian.com/BookReader/) (\\d+), (\\d+). aspx", "GMI");
Reg3.test ("http://www.qidian.com/BookReader/1017141,20361055.aspx");
Get three groups
alert (regexp.$1);
alert (regexp.$2);
alert (regexp.$3);
2, learn the most commonly used test exec match search replace split 6 methods
1 Test checks to see if the specified string exists
var data = "123123″;"
var recat =/123/gi;
Alert (recat.test (data)); True
Check to see if the character is present G continue to go down I case insensitive
2 Exec return query value
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
var recat =/cat/i;
Alert (recat.exec (data)); Cat
3 Match Gets the query array
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
var recat =/cat/gi;
var arrmactches = Data.match (recat)
for (Var i=0;i < arrmactches.length; i++)
{
Alert (Arrmactches[i]); Cat Cat
}
4 Search to return to a location similar to IndexOf
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
var recat =/cat/gi;
Alert (Data.search (Recat)); 23
5 Replace replacement character using regular substitution
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
var recat =/cat/gi;
Alert (Data.replace (Recat, "libinqq"));
6) split using a regular split array
var data = "123123,213,12312,312,3,cat,cat,dsfsdfs,";
var recat =/\,/;
var arrdata = Data.split (recat);
for (var i = 0; i < arrdata.length; i++)
{
Alert (Arrdata[i]);
}