How to find a partial String in another String with JavaScript

I've got a string lets say its "/s200/" which is part of a URL, and this part is responsible for resizing an image on demand. So whenever you change s200 to a higher or lower number following the s, then the picture that is returned by the complete URL will be either increased or decreased in size. Now i want to programmatically find this part in the url and replace it by lets say "/s800/" in order to make the returned image bigger.
1 answer

Replacing String in JavaScript with Regular Expression and the replace function

steps:

  1. searching the web for replacing texts
  2. finding regular expression
  3. searching for suitable regex function
  4. identifying replace out of (match, exec, replace) as the most suitable function to use
  5. searching for example how to use replace the right way
  6. finding and testing the right pattern that matches /s200/
  7. testing and iterating until the pattern matches the string and the modified string is ready to use

a javascript solution for that particular challenge looks the following way:


var testString = "http://www.google.at/img/s200/";
var partToBeReplaced = /s200/;
var newPart = "s1600";
var replacedTestString = testString.replace(partToBeReplaced, newPart);

Taggings: