JS replaces the space method in the string

  • 2020-05-30 19:23:48
  • OfStack


<input type=hidden name= " space " value= " &nbsp; " >

Usually in the input field & nbsp cannot be replaced & nbsp (space on page), if you want to replace it, you can use another method.
Add 1 hidden field with a value of & nbsp; And then you substitute


var sp=document.getElementById("space").value;
strData = document.all( "CommDN").value;
strData=strData.replace(sp,"");

js code


function formatStr(str)
{
str=str.replace(/\r\n/ig,"<br/>");
return str;
}

Two things to note:

To use regular expressions, str.replace ("\r\n", newString); , which causes only the first matching substring to be replaced.
In the parent string, not 1 dent \r\n will exist at the same time, maybe only \n, not \r.

The e syntax for the replace method is: stringObj.replace (rgExp, replaceText) where stringObj is a string (string), reExp can be a regular expression object (RegExp) or a string (string), replaceText is a substitute string found. And just to give you a sense of it, let me give you a simple example of 1

Js code


<script language="javascript"> 
var stringObj=" The final People's Republic, the final people "; 
// Replace the wrong character "forever" with "China"  
// And returns the replaced new character  
// The original string stringObj The value of  
var newstr=stringObj.replace(" forever "," China "); 
alert(newstr); 
</script>  

You who are smarter than me, after reading the above example, you will find that the second wrong word "zhonggu" has not been replaced with "China". We can execute replace method twice to replace the second wrong word "zhonggu" as well. After the process is improved, it is as follows:

Js code


<script language="javascript"> 
var stringObj=" The final People's Republic, the final people "; 
// Replace the wrong character "forever" with "China"  
// And returns the replaced new character  
// The original string stringObj The value of  
var newstr=stringObj.replace(" forever "," China "); 
newstr=newstr.replace(" forever "," China "); 
alert(newstr); 
</script>

We can think carefully 1, if there are N to N error, is it necessary to implement N to N replace method to replace the error? Oh,, don't be afraid. Once you have a regular expression, you need to execute the replace method once without a single error. The code of the improved program is as follows

Js code


<script language="javascript"> 
var reg=new RegExp(" forever ","g"); // Create a regular RegExp object  
var stringObj=" The final People's Republic, the final people "; 
var newstr=stringObj.replace(reg," China "); 
alert(newstr); 
</script> 

This is the simplest application of the replace method. So let's start with a slightly more complicated application. When you search for articles on some websites, you will find such a phenomenon, that is, the search keywords will be highlighted to change the color displayed?? How does this work? In fact, we can use regular expressions to implement, how to implement it? For a simple principle, see the code below

Js code


<script language="javascript"> 
var str=" The People's Republic of China, the People's Republic of China "; 
var newstr=str.replace(/( people )/g,"<font color=red>$1</font>"); 
document.write(newstr); 
</script> 

The above program is lack of interactivity, we improve the next 1 program, to achieve the automatic input to find the characters

Js code


<script language="javascript"> 
var s=prompt(" Please enter the character you are looking for "," people "); 
var reg=new RegExp("("+s+")","g"); 
var str=" The People's Republic of China, the People's Republic of China "; 
var newstr=str.replace(reg,"<font color=red>$1</font>"); 
document.write(newstr); 
</script> 

You u may not understand the meaning of the special character $1, in fact, $1 represents the left expression inside the parentheses of the character, that is, the first child match, the same way you can get $2 represents the second child match. What is a submatch? In layman's terms, is the left side of every 1 bracket is the first word match, the second bracket is the second child match. When we want to find the characters of the operation, how to achieve? Before e implementation, let's talk about how to get the parameters of a certain function. Inside the function Function, there is a set of arguments, which stores all the parameters of the current function. All the parameters of the function can be obtained through arguments. For your understanding, please see the following code

Js code


<script language="javascript"> 
function test(){ 
 alert(" Number of parameters: "+arguments.length); 
 alert(" every 1 Values of the following parameters: "+arguments[0]); 
 alert(" The first 2 The value of two parameters "+arguments[1]); 
 // You can use for The loop reads all the parameters  
} 
 
test("aa","bb","cc"); 
</script> 

After understanding the above program, let's take a look at the following interesting program

Js code


<script language="javascript"> 
var reg=new RegExp("\\d","g"); 
var str="abd1afa4sdf"; 
str.replace(reg,function(){alert(arguments.length);}); 
</script>  

We were surprised to find that the anonymous function was executed twice and took three parameters in the function. Why did it execute twice? This is easy to imagine, because the regular expression we wrote matches a single number, and the detected string also happens to have two Numbers, so the anonymous function is executed twice. What are the three parameters inside the anonymous function? To figure this out, let's look at the code below.

Js code


<script language="javascript"> 
function test(){ 
for(var i=0;i<arguments.length;i++){ 
 alert(" The first "+(i+1)+" Values of the following parameters: "+arguments); 
} 
} 
var reg=new RegExp("\\d","g"); 
var str="abd1afa4sdf"; 
str.replace(reg,test); 
</script>  

After observation, we find that the first parameter represents the matched character, the second parameter represents the minimum index position of the matched character (es1064en.index), and the third parameter represents the matched string (RegExp.input). In fact, the number of these parameters will also increase with the number of submatches. After we figure out these problems, we can write them the other way

Js code


<script language="javascript"> 
function test($1){ 
 return "<font color='red'>"+$1+"</font>" 
} 
var s=prompt(" Please enter the character you are looking for "," people "); 
var reg=new RegExp("("+s+")","g"); 
var str=" The People's Republic of China, the People's Republic of China "; 
var newstr=str.replace(reg,test); 
document.write(newstr); 
</script> 

After looking at the above program, you can do whatever you want with the matched characters. Here is a simple example of an application

Js code


<script language="javascript"> 
var str=" This year he 22 At the age of 18, she is this year 20 His father this year 45 Her father this year 44 Years old, 1 A total of 4 people " 
function test($1){ 
 var gyear=(new Date()).getYear()-parseInt($1)+1; 
 return $1+"("+gyear+" Born in )"; 
} 
var reg=new RegExp("(\\d+) At the age of ","g"); 
var newstr=str.replace(reg,test); 
alert(str); 
alert(newstr); 
</script>

That's all for this article, I hope you enjoy it.


Related articles: