Home  >  Article  >  Web Front-end  >  Problems encountered with the Replace() method in JS

Problems encountered with the Replace() method in JS

小云云
小云云Original
2018-01-04 13:27:101748browse

This article mainly introduces a summary of the problems encountered when using the Replace() method in JS. Friends who need it can refer to it. I hope it can help everyone.

Today I encountered several problems when writing the automated packaging script for the PC client. Although it was a minor problem, it was stuck for a while, so I decided to record it.

The replace() method of js is used to replace certain content. It can receive two parameters. The first one is a regular expression object to be replaced or a string, and the second one can be the to-be-replaced regular expression object. The content or function to be replaced must be a string. The error I encountered during execution was: Cannot read property ‘replace’ of undefined. The specific code is as follows:

var fs=require("fs")
var infoPlistFile = osxFolder + '/Contents/Info.plist';
var infoPlist = fs.readFile(infoPlistFile);
fs.writeFile(infoPlistFile, infoPlist.replace(/Pexip Infinity Connect/gm, $scope.manifest.name));

When executing this code, the error reported was that the undefined attribute replacement cannot be obtained. At first, I didn’t know where the problem was. Then I tried to print out the replaced file infoPlist and found that It is null, and then I know that there is a problem in defining infoPlist. After changing it to fs.readFileSync, I found that there is no such error, but there is a new error: replace is not a function

Find Looking at the relevant information, it shows that the corresponding variable is not a string, but another type, so I printed the type of infoPlist, and what was printed was object, so the problem lies here, because the type of infoPlist is wrong, which resulted in an error. , so I added the operation of processing it into a string, so it worked

var infoPlistFile = osxFolder + '/Contents/Info.plist';
var infoPlist = readFile(infoPlistFile).toString();
writeFile(infoPlistFile, infoPlist.replace(/Pexip Infinity Connect/gm, $scope.manifest.name));

In addition, when I wrote the demo myself for testing, I also found that if there is no variable to receive, the replace() method cannot be used Function, such as:

var str="Hello World";
str.replace(/World/g, "dxy");
console.log(str);

The str printed out at this time is still "Hello World" and has not been replaced. A variable should be used as the receiver here to replace the original variable, such as:

var str="Hello World";
var a=str.replace(/World/g, "dxy");
console.log(a);

The printed result at this time is "Hello dxy".

Summary: This time when using the replace() method, I noticed the following problems:

1. When using the replace() method, you must first make sure that it cannot be in null or Called on a variable of undefined type. We can add a judgment to it, and only call the method if the variable has a value.

2. When executing the replace() method, the variable must not only have a value, but also must be of string type.

3. JS does not automatically assign values. To execute a method, you must either execute it directly in the method or return the result. When the result is returned, it needs to be received through a variable.

4. If the first parameter is a regular expression, quotation marks cannot be added.

Related recommendations:

Usage examples of replace() method in Javascript

Deprecated in ECSHOP: preg_replace() error Solution

Introduction to the use of php string replacement str_replace() function

The above is the detailed content of Problems encountered with the Replace() method in JS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn