I have a SDP file and I convert it to string like bellow:
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
And in this line " m=video 20000 RTP/AVP 96 " I want to change '20000' to '4321',I wrote this bellow code:
let fileds= inputString .split(/\s+/);
for(const field of fields) {
if(field === "m=video") {
}
if (field === "m=audio") {
var myflag = "au";
}
}
and above ode give me this bellow result:
m=video 4321 RTP/AVP 96
m=video 4321 RTP/AVP 96
I just want change them and replace to the string(like new string),Imagine I need result like bellow:
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
I totally new in java script, How I can replace it? because maybe I have another string with different "m=video" index in here 'm' in line 2 maybe in next string "m=video" on line 3 or 4.
Can I use ' string.startswith('m=video') ' and where I this code it true replace it with what I got it
EX : m=video 4321 RTP/AVP 96
I have an example:
Either simply using replace all inputString.replace(/m=video 20000/g, "m=video 4321")
, where the g in /m=video 20000/g
means replace globally (i.e. replace all).
Or we can map over each line and replace each line with stringArr.map(str => str.replace(/m=video 20000/, "m=video 4321"))
To make it dynamic (i.e. allow other numbers other than 20000) we can create a RegExp:
const inputNumberString = '20000'; //can be changed
const regex = new RegExp('m=video ' + inputNumberString, 'g');
const stringReplacedAll = inputString.replace(regex, "m=video 4321")
Or if the same pattern is always present but different numbers and you want to match any numbers you can use a RegExp pattern match like:
const inputPattern= /m=video \d{5}/g;
const stringReplacedAll = inputString.replace(inputPattern, "m=video 4321")
To change other parts of the string like the IP, do something like this:
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");
Which matches any 5 digit number after 'm=video '
;
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);