更改字符串并将其替换为我搜索的特定索引并制作新字符串

Change the string and replace it in the specific index that I search for it and make new string

我有一个 SDP 文件,我将其转换为如下所示的字符串:

v=0
o=- 1443716955 1443716955 IN IP4 10.20.130.110
m=video 20000 RTP/AVP 96 // <== this line 
c=IN IP4 239.0.1.2/64
a=source-filter: incl IN IP4 239.0.1.2 192.168.0.1
a=rtpmap:96 raw/90000
a=mid:primary
m=video 20000 RTP/AVP 96 // <== this line 
c=IN IP4 239.0.1.3/64
a=source-filter: incl IN IP4 239.0.1.3 192.168.0.1

在这一行“m=video 20000 RTP/AVP 96”中,我想将 '20000' 更改为 '4321',我写了下面这段代码:

 let fileds= inputString .split(/\s+/);
    for(const field of fields) {
     if(field === "m=video") {

      }
     if (field === "m=audio") {
          var myflag = "au";
     }
    }       

及以上颂歌给我以下结果:

m=video 4321 RTP/AVP 96
m=video 4321 RTP/AVP 96

我只想更改它们并替换为字符串(如新字符串),假设我需要如下结果:

v=0
o=- 1443716955 1443716955 IN IP4 10.20.130.110
m=video 4321 RTP/AVP 96 // <== this line
c=IN IP4 239.0.1.2/64
a=source-filter: incl IN IP4 239.0.1.2 192.168.0.1
a=rtpmap:96 raw/90000
a=mid:primary
m=video 4321 RTP/AVP 96 //<== this line
c=IN IP4 239.0.1.3/64
a=source-filter: incl IN IP4 239.0.1.3 192.168.0.1

我在 java 脚本中是全新的,我该如何替换它?因为也许我在第 2 行的 'm' 中有另一个具有不同 "m=video" 索引的字符串,也许在第 3 行或第 4 行的下一个字符串 "m=video" 中。

我可以使用“string.startswith('m=video')”吗?我把这段代码替换成我得到的代码

EX : m=视频 4321 RTP/AVP 96

我有一个例子:

要么简单地使用替换所有 inputString.replace(/m=video 20000/g, "m=video 4321"),其中 /m=video 20000/g 中的 g 表示全局替换(即替换所有)。

或者我们可以映射每一行并将每一行替换为 stringArr.map(str => str.replace(/m=video 20000/, "m=video 4321"))

要使其动态化(即允许 20000 以外的其他数字),我们可以创建一个 RegExp:

const inputNumberString = '20000'; //can be changed
const regex = new RegExp('m=video ' + inputNumberString, 'g');
const stringReplacedAll = inputString.replace(regex, "m=video 4321")

或者,如果始终存在相同的模式但数字不同,并且您想匹配任何数字,您可以使用 RegExp 模式匹配,例如:

const inputPattern= /m=video \d{5}/g;
const stringReplacedAll = inputString.replace(inputPattern, "m=video 4321")

要更改字符串的其他部分(如 IP),请执行以下操作:

const inputPattern= /m=video \d{5}/g;
const ipPattern = /c=IN IP4 \d{3}.\d.\d.\d\/\d{2}/g;
const stringReplacedAll = inputString
                    .replace(inputPattern, "m=video 4321")
                    .replace(ipPattern, "c=IN IP4 123.0.1.1/88");

匹配'm=video '后任意5位数字;

const inputString = 
`v=0
o=- 1443716955 1443716955 IN IP4 10.20.130.110
m=video 20000 RTP/AVP 96 // <== this line 
c=IN IP4 239.0.1.2/64
a=source-filter: incl IN IP4 239.0.1.2 192.168.0.1
a=rtpmap:96 raw/90000
a=mid:primary
m=video 20000 RTP/AVP 96 // <== this line 
c=IN IP4 239.0.1.3/64
a=source-filter: incl IN IP4 239.0.1.3 192.168.0.1`;

const inputNumberString = '20000';
const inputPattern= /m=video \d{5}/g;
const ipPattern = /c=IN IP4 \d{3}.\d.\d.\d\/\d{2}/g;
const regex = new RegExp('m=video ' + inputNumberString, 'g');
const stringReplacedAll = inputString.replace(inputPattern, "m=video 4321").replace(ipPattern, "c=IN IP4 123.0.1.1/88");

console.log(stringReplacedAll);

//OR you can do it with array and map
const stringArr = inputString.split('\n');
const stringReplacedMap = stringArr.map(str => str.replace(/m=video 20000/, "m=video 4321")).join('\n');

//console.log(stringReplacedMap);