-5

Am trying to replace a particular string from all lines using javascript. Here is sample

 www.youtube.com/watch?v=s8IJkexsjlI&index=37&list=PLB03EA9545DD188C3
 www.youtube.com/watch?v=PYOQLPW6ewQ&index=38&list=PLB03EA9545DD188C3
 www.youtube.com/watch?v=ejRmaOHs1rk&index=39&list=PLB03EA9545DD188C3

I want to replace the 11 characters after '=' how can I do it with a variable like var= 'hello123456'

so output will be

www.youtube.com/watch?v=hello123456&index=37&list=PLB03EA9545DD188C3
www.youtube.com/watch?v=hello123456&index=38&list=PLB03EA9545DD188C3
www.youtube.com/watch?v=hello123456&index=39&list=PLB03EA9545DD188C3
sravya naidu
  • 39
  • 1
  • 8
  • 2
    show what have u tried – Avinash Roy May 22 '17 at 12:45
  • Did you try to use string methods? like `replace`, `substring`? – Tushar May 22 '17 at 12:45
  • seems like a basic regular expression – epascarello May 22 '17 at 12:47
  • Your question is incomplete. `so output will be`??? Also you have failed to specify what is this 11 characters or after which `=`. You have also not shared any effort. You can look into `string.replace`, `string.split` and `string.substring` but please give it a try – Rajesh May 22 '17 at 12:48
  • 1
    You're being serial downvoted because you haven't shown any attempt to answer the question on your own, if you have tried, show us the non-working code, so we have a starting point on how you're trying to address this. – user2366842 May 22 '17 at 12:49
  • Possible Duplicate: https://stackoverflow.com/questions/1162529/javascript-replace-regex – Rajesh May 22 '17 at 12:54

3 Answers3

1

Here is regex solution, see the snippet below

document.addEventListener("DOMContentLoaded", function(){

 var link = "www.youtube.com/watch?v=s8IJkexsjlI&index=37&list=PLB03EA9545DD188C3";
 var value = "hello123456";
 link = link.replace(/\?v=(.*)\&index/g, "?v="+value+"&index")
 document.getElementById("id").innerHTML = link

});
Input link:
<div>www.youtube.com/watch?v=s8IJkexsjlI&index=37&list=PLB03EA9545DD188C3</div>
<br>
<br>
Output link:
<div id="id"></div>
Nebojsa Nebojsa
  • 1,395
  • 1
  • 7
  • 19
0

This is pretty straightforward solution, without any fanciness so you could easy understand what's happening.

var originalStr = 'www.youtube.com/watch?v=s8IJkexsjlI&index=37&list=PLB03EA9545DD188C3';
var textToReplaceWith = 'hello123456';
var newStr = '';

// get index where unwanted text starts
var index = originalStr.indexOf('v=') + 2;

newStr = originalStr.substring(0, index); //take part just before the unnecessary text
newStr += textToReplaceWith; //add string you want
newStr +=  originalStr.substring(index + 11, originalStr.length) // take text part after unwanted text
Mantas Čekanauskas
  • 2,218
  • 6
  • 23
  • 43
  • 1
    Regex is always better in such case. `/v=[^&]*(?=&|$)/` – Rajesh May 22 '17 at 12:58
  • @Rajesh your regex catch a whole string https://regex101.com/r/PsAdJM/1 – Nebojsa Nebojsa May 22 '17 at 13:02
  • @Rajesh - depends on how you define "better". Personally I usually prefer to avoid regex, even if it is a slight performance hit for the simple sake of readability. Maintaining string replacements is easier. – user2366842 May 22 '17 at 17:49
  • @user2366842 in that case, you will loop through string and find index of `v=`. Then you will take first half in temp and again loop in the remaining string for index of `&`. And finally you will do another substring on remaining string to replace value. Plus if this behavior is to be repeated multiple times, you will make it a function and call it manually. Regret takes care of all this. Yes not everyone is good in regex, but sometimes it better to add comment on such places to maintain readability. :-) – Rajesh May 23 '17 at 03:34
0

Please check the below code

var youstr="www.youtube.com/watch?v=s8IJkexsjlI&index=37&list=PLB03EA9545DD188C3";
var start=youstr.indexOf("=");
var end=start+11;
var replaceStr=youstr.slice(start,start+11);
youstr=youstr.replace(replaceStr,"ReplaceString");