SoFunction
Updated on 2025-03-03

JS method to replace spaces in strings

Copy the codeThe code is as follows:

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

Usually, the input field cannot be replaced (there is a space in the source code, and the page is displayed as a space). If you want to replace it, you can use other means.
Add a hidden field with the value &nbsp; and then replace it

Copy the codeThe code is as follows:

var sp=("space").value;
strData = ( "CommDN").value;
strData=(sp,"");

js code

Copy the codeThe code is as follows:

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

Two things to note:

To use regular expressions, you cannot use ("\r\n", newString); , which causes only the first matching substring to be replaced.
It is not necessarily true that \r\n will exist at the same time in the alphabetical string, maybe only \n, and it is also possible that no \r is not.

The syntax of the replace method is: (rgExp, replaceText) where stringObj is a string (string), reExp can be a regular expression object (RegExp) or a string (string), replaceText is a string instead of the found string. . To help you understand better, let me give you a simple example to illustrate

Js code

&lt;script language="javascript"&gt; 
var stringObj="The People's Republic of the Ancient Times, the People of the Ancient Times"; 
//Replace the typo "Zhonggu" to "China"// and return the replaced new character//The value of the original string stringObj has not changedvar newstr=("The End of the Ancient","China"); 
alert(newstr); 
&lt;/script&gt;  

You who are smarter than me, after reading the above example, you will find that the second typo "Zhonggu" has not been replaced with "China". We can execute the secondary replace method to replace the second typo "Zhonggu". After the program is improved, it is as follows:

Js code

&lt;script language="javascript"&gt; 
var stringObj="The People's Republic of the Ancient Times, the People of the Ancient Times"; 
//Replace the typo "Zhonggu" to "China"// and return the replaced new character//The value of the original string stringObj has not changedvar newstr=("The End of the Ancient","China"); 
newstr=("The End of the Ancient","China"); 
alert(newstr); 
&lt;/script&gt;

We can think carefully about it. If there is a typo in N power of N, should we also execute the N power of N replacement method to replace the typo? ? Oh, don't be afraid, after having regular expressions, you need to execute the replace method without a typo. . The code after the program has been improved is as follows

Js code

&lt;script language="javascript"&gt; 
var reg=new RegExp("The End of the Ancient","g"); //Create a regular RegExp objectvar stringObj="The People's Republic of the Ancient Times, the People of the Ancient Times"; 
var newstr=(reg,"China"); 
alert(newstr); 
&lt;/script&gt; 

The above is about the simplest application of the replace method. I wonder if you understand it? ? Let’s start with a slightly more complex application. . When you search for articles on some websites, you will find a phenomenon that the search keywords will be highlighted and displayed with a change in color? ? How is this achieved? ? In fact, we can use regular expressions to implement it. How to implement it specifically? For simple principles, please see the code below

Js code

&lt;script language="javascript"&gt; 
var str="People's *, the People's *"; 
var newstr=(/(people)/g,"&lt;font color=red&gt;$1&lt;/font&gt;"); 
(newstr); 
&lt;/script&gt; 

The above program lacks interactivity. Let's improve the program to enable the ability to automatically enter the characters to be found.

Js code

&lt;script language="javascript"&gt; 
var s=prompt("Please enter the character you are looking for","people"); 
var reg=new RegExp("("+s+")","g"); 
var str="People's *, the People's *"; 
var newstr=(reg,"&lt;font color=red&gt;$1&lt;/font&gt;"); 
(newstr); 
&lt;/script&gt; 

Maybe everyone doesn't understand what the special character $1 means. In fact, $1 represents the character in brackets in the expression on the left, that is, the first sub-match. Similarly, $2 represents the second sub-match. . What is sub-match? ? In layman's terms, each bracket on the left is the first word match, and the second bracket is the second sub-match. . How to implement it when we want to calculate the found characters? ? Before implementing it, 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 parameters of the current function. All parameters of the function can be obtained through arguments. For everyone's understanding, please see the following code

Js code

&lt;script language="javascript"&gt; 
function test(){ 
 alert("Number of parameters:"+); 
 alert("Value of each parameter:"+arguments[0]); 
 alert("Value of the second parameter"+arguments[1]); 
 //You can use a for loop to read all parameters} 
 
test("aa","bb","cc"); 
&lt;/script&gt; 

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"; 
(reg,function(){alert();}); 
</script>  

We were surprised to find that anonymous functions were actually executed quadratic and had three parameters in the function. Why was they executed quadratic? ? This is easy to think of, because the regular expression we write matches a single number, and the detected string happens to have two numbers, the anonymous function is executed quadratic. . What are the three parameters inside anonymous functions? ? To figure out this problem, let's look at the following code.

Js code

&lt;script language="javascript"&gt; 
function test(){ 
for(var i=0;i&lt;;i++){ 
 alert("Third"+(i+1)+"Value of parameters:"+arguments); 
} 
} 
var reg=new RegExp("\\d","g"); 
var str="abd1afa4sdf"; 
(reg,test); 
&lt;/script&gt;  

After observation, we found that the first parameter represents the matching character, the second parameter represents the minimum index position of the character when the match (), and the third parameter represents the matching string (). In fact, the number of these parameters will increase with the number of sub-matches. After understanding these problems, we can use another way of writing

Js code

&lt;script language="javascript"&gt; 
function test($1){ 
 return "&lt;font color='red'&gt;"+$1+"&lt;/font&gt;" 
} 
var s=prompt("Please enter the character you are looking for","people"); 
var reg=new RegExp("("+s+")","g"); 
var str="People's *, the People's *"; 
var newstr=(reg,test); 
(newstr); 
&lt;/script&gt; 

After reading the above program, it turns out that you can do whatever you want with the matching characters. Here is a brief application example

Js code

&lt;script language="javascript"&gt; 
var str="He is 22 years old, she is 20 years old, his father is 45 years old, her father is 44 years old, and there are 4 people in total." 
function test($1){ 
 var gyear=(new Date()).getYear()-parseInt($1)+1; 
 return $1+"("+gyear+"Born in 2018)"; 
} 
var reg=new RegExp("(\\d+)age","g"); 
var newstr=(reg,test); 
alert(str); 
alert(newstr); 
&lt;/script&gt;

The above is the entire content of this article, I hope you like it.