Summary of javascript's replace method combined with regular usage examples

  • 2021-06-28 10:56:49
  • OfStack

This paper summarizes the replace method of javascript combined with regular usage.Share it for your reference, as follows:

The replace() method is used to replace one character with another in a string, or a string matched by a regular expression.

Example 1: Replace directly with repalce


var stringObj=" The People's Republic of Ancient Times, the People of Ancient Times ";
// Replace the wrong alias "Ancient" with "China" 
// And returns the replaced new character 
// Original string stringObj The value of 
var newstr=stringObj.replace(" Ancient "," China ");
// The People's Republic of China, the Ancient People 
alert(newstr);

Example 2: Replace all with regular expressions


var str=" The People's Republic of Ancient Times, the People of Ancient Times ";
var newstr=str.replace(/( Ancient )/g," China ");
// The People's Republic of China, the People's Republic of China 
alert(newstr);

Equivalent to


var reg=new RegExp(" Ancient ","g"); // Create a regular RegExp object 
var stringObj=" The People's Republic of Ancient Times, the People of Ancient Times ";
var newstr=stringObj.replace(reg," China ");
alert(newstr);

Example 3: Regular expressions, variable matching


var resource=" Ancient ";
var target=" China ";
var reg=new RegExp(resource,"g"); // Create a regular RegExp object 
var stringObj=" The People's Republic of Ancient Times, the People of Ancient Times ";
var newstr=stringObj.replace(reg,target);
alert(newstr);

Example 4: Regular grouping matching


var strM = "javascript is a good script language";
//$1 Matching is javascript,$2 Matching is is
// The final value returned is "javascript is fun. it is" + strM
// That is javascript is  Replaced with  javascript is fun. it is
alert(strM.replace(/(javascript)\s*(is)/g,"$1 $2 fun. it $2"));

Example 5: Using callback functions for detailed processing


var name="aaa bbb ccc";
//name String Unmatch /\b\w+\b/g Expression, the result is 3 Each - aaa,bbb,ccc ;Execution of each result function Method inside 
var uw = name.replace(/\b\w+\b/g,function(word){
  //word Is a matching string 
  alert(word);
 return word.substring(0,1).toUpperCase()+word.substring(1);
});
alert(uw);

Example 6: More obscure writing


var reg=new RegExp("(http://www.qidian.com/BookReader/)(\\d+),(\\d+).aspx","gmi");
var url="http://www.qidian.com/BookReader/1017141,20361055.aspx";
// mode 1, The easiest and most common way 
var rep=url.replace(reg,"$1ShowBook.aspx?bookId=$2&chapterId=$3");
alert(rep);
// mode 2 , Callback functions with fixed parameters 
var rep2=url.replace(reg,function(m,p1,p2,p3){
 return p1+"ShowBook.aspx?bookId="+p3+"&chapterId="+p3
});
alert(rep2);
// mode 3 With a callback function with no 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 Represents a space, then \S Represents a non-space, so /(\S+)(\s+)(\S+)/g Matches the result of "Non-whitespace Non-whitespace" 
 // The matched results are The rain , in Spain , falls mainly , in the
 // Replaced results are rain The , Spain in , mainly falls , the in
 re = /(\S+)(\s+)(\S+)/g;  //  Create a regular expression pattern. 
 // Change the order between matching results 
 r = ss.replace(re, "$3$2$1"); //  Swap every 1 For words. 
 return(r);      //  Returns 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 more convenient regular expression tools for your reference:

JavaScript Regular Expression Online Test Tool:
http://tools.ofstack.com/regex/javascript

Regular Expression Online Generation Tool:
http://tools.ofstack.com/regex/create_reg

More readers interested in JavaScript-related content can view this site's topics: Summary of JavaScript Switching Effects and Techniques, Summary of JavaScript Finding Algorithmic Techniques, Summary of JavaScript Animation Effects and Techniques, Summary of JavaScript Errors and Debugging Techniques, Summary of JavaScript Data Structure and Algorithmic Techniques.Summary of JavaScript Traversal Algorithms and Techniques and Summary of JavaScript Mathematical Operation Usage

I hope that the description in this paper will be helpful to everyone's JavaScript program design.


Related articles: