This example summarizes the replacement method of JavaScript combined with regular usage methods. Share it for your reference, as follows:
Replace() method is used to replace some characters in a string, or replace a string matched by a regular expression
Example 1: Use replacement directly
var stringObj="Zhonggu People's Republic, Zhonggu People";//Replace the typo "Zhonggu" to "China"// and return the replaced new character//The value of the original string stringObj has not changed var newsstr=stringObj.replace("Zhonggu","China");//The People's Republic of China, Zhonggu People's alert(newstr);Example 2: Use regular expressions and replace them all
var str="The People's Republic of the Year, the People's Republic of the Year"; var newsstr=str.replace(/(The People's Republic of the Year)/g,"China");//The People's Republic of China, the People's Republic of China alert(newstr);
Equivalent to
var reg=new RegExp("Ending the Age","g"); //Create a regular RegExp object var stringObj="Ending the Age People's Republic,Ending the Age People"; var newsstr=stringObj.replace(reg,"China");alert(newstr);Example 3: Regular expressions, variable matching
var resource="一年年";var target="一年";var reg=new RegExp(resource,"g"); //Create a regular RegExp object var stringObj="一年年年年年";var newsstr=stringObj.replace(reg,target);alert(newstr);
Example 4: Regular grouping matching
var strM = "javascript is a good script language";//$1 matches javascript, $2 matches is//The final return value is "javascript is fun. it is" + strM//That is, javascript is replaced with javascript is fun. it isalert(strM.replace(/(javascript)/s*(is)/g,"$1 $2 fun. it $2"));
Example 5: Use callback function for detailed processing
var name="aaa bbb ccc";//name string matches the //b/w+/b/g expression, and there are three results - aaa,bbb,ccc; each result executes the method in function var uw = name.replace(//b/w+/b/g,function(word){ //word is the matching string alert(word); return word.substring(0,1).toUpperCase()+word.substring(1);});alert(uw);Example 6: A more uncommon way of writing
var reg=new RegExp("(http://www.qidian.com/BookReader/)(//d+),(//d+).aspx","gmi");var url="http://www.qidian.com/BookReader/1017141,20361055.aspx";//Method 1, the simplest and most commonly used method var rep=url.replace(reg,"$1ShowBook.aspx?bookId=$2&chapterId=$3");alert(rep);//Method 2, the callback function with fixed parameters var rep2=url.replace(reg,function(m,p1,p2,p3){ return p1+"ShowBook.aspx?bookId="+p3+"&chapterId="+p3});alert(rep2);//Method 3, use the callback function with non-fixed parameters var rep3=url.replace(reg,function(){ var args=arguments; return args[1]+"ShowBook.aspx?bookId="+args[2]+"&chapterId="+args[3];});alert(rep3); function ReplaceDemo(){ var r, re; // Declare variables. var ss = "The rain in Spain falls mainly in the plain."; // /s means space, then /S means non-space, so /(/S+)(/s+)(/S+)/g matches the result of "non-space space, non-space" //The matching result includes The rain, in Spain, falls mainly, in the //The replacement result includes rain The, Spain in, mainly falls, the in re = /(/S+)(/s+)(/S+)/g; // Create regular expression pattern. //Change the order between matching results r = ss.replace(re, "$3$2$1"); //Swap each pair of words. return(r); // Return the result string. }alert(ReplaceDemo());name = "Doe, John";//Change the order between two words var temp = name.replace(/(/w+)/s*, /s*(/w+)/, "$2 $1");alert(temp);
function SDReplaceData(objStr){ return objStr.replace( /(/&|/')/g, function($0, $1) { return{ "&" : "&" , "'" : "'" }[$1]; } );}PS: Here are two very convenient regular expression tools for your reference:
JavaScript regular expression online testing tool:
http://tools.VeVB.COM/regex/javascript
Regular expression online generation tool:
http://tools.VeVB.COM/regex/create_reg
For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm skills", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm skills", "Summary of JavaScript traversal algorithms and techniques", and "Summary of JavaScript mathematical operations usage"
I hope this article will be helpful to everyone's JavaScript programming.