修改“字符串” txt doc里面的值 [英] Modify "string" value inside txt doc

查看:64
本文介绍了修改“字符串” txt doc里面的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用readFile方法读取了一个文本文档,我得到了当前的字符串。
我需要修改里面的一些特定字段并保存文件并保留原始格式

I Read a text document with readFile method ,and I got the current string. I need to modify some specific fields inside and save the file and keep the original format

在读取文件中我得到以下字符串,我需要更改只是属性 INSTANCE_ADDR 的数字从3000到4000并保持相同的格式,问题是值3000可以是任何数字所以我需要找到键 INSTANCE_ADDR 然后以某种方式更新值并保持相同的格式,我应该怎么做?

in the read file I got the following string and I need to change just the number for property INSTANCE_ADDR the value from 3000 to 4000 and keep it in the same format,the problem is that the value 3000 can be any number so I need to finde the key INSTANCE_ADDR and then somehow to update the value and keep it in same format,how should I do that ?

"BASH_VERSINFO=([0]=\"3\" [1]=\"2\" [2]=\"51\" [3]=\"1\" [4]=\"release\" [5]=\"x86_64-suse-linux-gnu\")\r\nBASH_VERSION='3.2.51(1)-release'\r\nINSTANCE_ADDR=10.97.27.7:3000\r\\r\n"

我使用 fs.readFile(filePath,'utf8'),响应就是这个内容

I use the fs.readFile(filePath, 'utf8') and the response is this content

推荐答案

(INSTANCE_ADDR=[^:]*):\d+

试试这个。替换 $ 1:4000 参见演示。

Try this.Replace by $1:4000 See demo.

https://regex101.com/r/uF4oY4/55

var re = /(INSTANCE_ADDR=[^:]*):\d+/gi; 
var str = '"BASH_VERSINFO=([0]=\"3\" [1]=\"2\" [2]=\"51\" [3]=\"1\" [4]=\"release\" [5]=\"x86_64-suse-linux-gnu\")\r\nBASH_VERSION=\'3.2.51(1)-release\'\r\nINSTANCE_ADDR=10.97.27.7:3000\r\\r\n"';
var subst = '$1:4000'; 

var result = str.replace(re, subst);

内部

var re = /(CF_INSTANCE_PORTS='\[.*?internal):\d+/gi; 
var str = 'CF_INSTANCE_PORTS=\'[{external:50100,internal:50100}]';
var subst = '$1:12312'; 

var result = str.replace(re, subst);

这篇关于修改“字符串” txt doc里面的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆