【问题标题】:Change the string and replace it in the specific index that I search for it and make new string更改字符串并在我搜索它的特定索引中替换它并创建新字符串
【发布时间】:2020-07-09 03:07:42
【问题描述】:

我有一个 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";
     }
    }       

及以上 ode 给我以下结果:

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=video 4321 RTP/AVP 96

【问题讨论】:

    标签: javascript arrays string replace startswith


    【解决方案1】:

    我有一个例子:

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

    或者我们可以映射每一行并将每一行替换为stringArr.map(str =&gt; 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);

    【讨论】:

    • 如果 '20000' 不是常数,我该如何改变它?
    • 我更新了我的答案。您需要开始使用 RegExp,并且可以使用您的客户编号字符串构建 RegExp,也可以匹配 5 个数字之类的模式。请参阅我的更新答案。另外,developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    • 如果您有任何问题,请告诉我。我的回答能解决你的问题吗?
    • 那太好了,'c'怎么样 你对IP有什么想法吗?我把这段代码与你的代码相似,但没有回答: const inputPatternC = /c=IN IP4 \d{4}/g; const regex_c = new RegExp('c=IN IP4' + FinalIP, 'g'); const stringReplacedC = stringReplacedMv.replace(inputPatternC, "c=IN IP4"+" "+ FinalIP);
    • 您可以使用 const ipPattern = /c=IN IP4 \d{3}.\d.\d.\d\/\d{2}/g; 之类的东西来修补 IP 字符串并替换它。请参阅我的更新答案。如果我已经解决了您的问题,请考虑投票并标记为已接受的答案。
    猜你喜欢
    • 2011-04-13
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    相关资源
    最近更新 更多