-4

I need to do a string substitution/replacement via regex and I just can't figure it out.

The string is in the following format: http[s]://assets.example.com/images/[8 or 9 alphanumberic chars]m/[ 8 or 9 different alphanumeric chars].jpg

The character I want to replace is the m in the middle of the 2 alphanumeric strings.

Here's an example of what I'm trying to do.

http://assets.example.com/images/11233434m/23435436.jpg and I want to change it into http://assets.example.com/images/11233434l/23435436.jpg

I'm familiar with str.replace, I just can't come up with a regex that works, without replacing incorrect characters (like the last m in example.com/)

Jason Kulatunga
  • 5,606
  • 1
  • 23
  • 50
  • sigh. Yes I know its a pretty simple question for SO, but I did a pretty indepth search first, and I couldn't find anything similar. Would love to know why it deserves a downvote.. – Jason Kulatunga Jul 20 '14 at 02:04
  • 1
    It's not that useful to future readers. Maybe if you invest in using a capturing group you can put it together. There are no advanced techniques here, since it's [[tag:javascript]]. – Unihedron Jul 20 '14 at 02:08
  • Your question is very clearly written. I think the downvotes are for not showing your attempts. – aliteralmind Jul 20 '14 at 02:19

4 Answers4

2

The following code should work:

var str1 = "http://assets.example.com/images/11233434m/23435436.jpg";
var patt = /(http:\/\/assets\.example\.com\/images\/[a-zA-Z0-9]{8,9})m(\/[a-zA-Z0-9]{8,9}\.jpg)/;
var mats = patt.exec(str);
var str2 = mats[1]+"l"+mats[2];

The logic is simple. Because [a-zA-Z0-9]{8,9} matches eight or nine alphanumeric characters, the regular expression patt in my code matches a string exactly as described by you. It also captures the part of the string before the incorrect m and the part of the string after it. Then we just concatenate the first part, an l and the second part.

Sharanya Dutta
  • 3,883
  • 2
  • 14
  • 27
2

try this to see if it is help or not

var url = "http://assets.example.com/images/11233434m/23435436.jpg";
url.replace(/images\/([a-zA-Z0-9]{8,9})(m)\//, function(total, g1, g2) {
    //console.log(total, g1, g2);
    return 'images/' + g1 + "l/";
})
powerfj
  • 136
  • 6
1

Here is a regex that works:

var str = 'http://assets.example.com/images/11233434m/23435436.jpg';
var replaced = str.replace(/(https?:\/\/assets\.example\.com\/images\/[a-zA-Z0-9]{8,9})m(\/[a-zA-Z0-9]{8,9}\.jpg)/, '$1l$2')
techfoobar
  • 61,046
  • 13
  • 104
  • 127
1

I believe this does as you wish

  • Find what: (https?:\/\/assets\.example\.com\/images\/[a-zA-Z0-9]{8,9})m(\/[a-zA-Z0-9]{8,9}\.jpg)
  • Replace with: $1l$2
  • Try it: http://regex101.com/r/oO4sO5/4

Free spaced:

(http                   #Open capture, "http"
s?:\/\/                 #Optional "s", required "://"
assets\.example\.com\/images\/   #literal portion
[a-zA-Z0-9]{8,9})       #8-or-9 alpha-num chars, end capture
m                       #"m"
(\/                     #Start capture, "/"
(?:[a-zA-Z0-9]{8,9})\.jpg)   #8-or-9 alpha-num chars, ".jpg", end capture

Please consider bookmarking the Stack Overflow Regular Expressions FAQ for future reference.

Community
  • 1
  • 1
aliteralmind
  • 18,274
  • 16
  • 66
  • 102
  • 1
    Using `\Q \E` literal sequence would make the literal portion more readable, and +1 for Regex FAQ. – Unihedron Jul 20 '14 at 02:10
  • Good point, @Unihedron. regex101 doesn't seem to accept `\Q..\E`, so I'm going to leave it out. But you're right, it would make it more readable. – aliteralmind Jul 20 '14 at 02:13