多字符串替换功能在nodejs中不起作用
multiple string replace function not working in nodejs
这里我需要将来自 json
对象的多个文件中的 #tenant_id#
替换为 1.
这是要替换的第一个文件#tenant_id#
:
INSERT INTO mst_designation (designation_name,tenant_id)
SELECT designation_name,#tenant_id# tenant_id FROM mst_designation mdes
WHERE tenant_id = -1
AND NOT EXISTS (SELECT 1 FROM mst_designation
WHERE designation_name = mdes.designation_name AND tenant_id = #tenant_id#);
这里第一个#tenant_id#
替换成1
第二个不替换
替换字符串的代码:
const dirPath = path.join(__dirname + '/script/')
filenames = fs.readdirSync(dirPath)
filenames.forEach(files => {
file = fs.readFileSync(dirPath + files, { encoding: 'utf8', flag: 'r' })
fileReplace = file.replace("#tenant_id#", new_tenant_id)
logger.log('silly', 'tenantOnBoard : tenant_id replace : Start ' + fileReplace);
const queryCreateTenant = conPool.query(fileReplace)
console.log(queryCreateTenant);
}
这是替换字符串的代码,这里的操作是
INSERT INTO mst_designation (designation_nametenant_id)
SELECT designation_name,#tenant_id# tenant_idFROM mst_designation mdes
WHERE tenant_id = -1
AND NOT EXISTS (SELECT 1 FROM mst_designation
WHERE designation_name = mdes.designation_name AND tenant_id = #tenant_id#);
#tenant_id#没有取代第二名
如何解决这个问题
这是 javascript 中的预期行为。要多次替换字符串,您可以使用带有 g
标志的 RegExp:
fileReplace = file.replace(/#tenant_id#/g, new_tenant_id)
你必须使用 replaceAll()
。
var query = "INSERT INTO mst_designation (designation_name,tenant_id) "+
"SELECT designation_name,#tenant_id# tenant_id FROM mst_designation mdes "+
"WHERE tenant_id = -1 AND NOT EXISTS (SELECT 1 FROM mst_designation WHERE designation_name = "+ " mdes.designation_name AND tenant_id = #tenant_id#);"
var fileReplace = query.replaceAll("#tenant_id#", "new_replaced_id")
console.log(fileReplace)
这个名字是不言自明的。使用 replaceAll()
您将替换所有与第一个 insetad 匹配的实例。
这里我需要将来自 json
对象的多个文件中的 #tenant_id#
替换为 1.
这是要替换的第一个文件#tenant_id#
:
INSERT INTO mst_designation (designation_name,tenant_id)
SELECT designation_name,#tenant_id# tenant_id FROM mst_designation mdes
WHERE tenant_id = -1
AND NOT EXISTS (SELECT 1 FROM mst_designation
WHERE designation_name = mdes.designation_name AND tenant_id = #tenant_id#);
这里第一个#tenant_id#
替换成1
第二个不替换
替换字符串的代码:
const dirPath = path.join(__dirname + '/script/')
filenames = fs.readdirSync(dirPath)
filenames.forEach(files => {
file = fs.readFileSync(dirPath + files, { encoding: 'utf8', flag: 'r' })
fileReplace = file.replace("#tenant_id#", new_tenant_id)
logger.log('silly', 'tenantOnBoard : tenant_id replace : Start ' + fileReplace);
const queryCreateTenant = conPool.query(fileReplace)
console.log(queryCreateTenant);
}
这是替换字符串的代码,这里的操作是
INSERT INTO mst_designation (designation_nametenant_id)
SELECT designation_name,#tenant_id# tenant_idFROM mst_designation mdes
WHERE tenant_id = -1
AND NOT EXISTS (SELECT 1 FROM mst_designation
WHERE designation_name = mdes.designation_name AND tenant_id = #tenant_id#);
#tenant_id#没有取代第二名 如何解决这个问题
这是 javascript 中的预期行为。要多次替换字符串,您可以使用带有 g
标志的 RegExp:
fileReplace = file.replace(/#tenant_id#/g, new_tenant_id)
你必须使用 replaceAll()
。
var query = "INSERT INTO mst_designation (designation_name,tenant_id) "+
"SELECT designation_name,#tenant_id# tenant_id FROM mst_designation mdes "+
"WHERE tenant_id = -1 AND NOT EXISTS (SELECT 1 FROM mst_designation WHERE designation_name = "+ " mdes.designation_name AND tenant_id = #tenant_id#);"
var fileReplace = query.replaceAll("#tenant_id#", "new_replaced_id")
console.log(fileReplace)
这个名字是不言自明的。使用 replaceAll()
您将替换所有与第一个 insetad 匹配的实例。