-4

I have some string like this:

http://my-site.com/name-274x183.jpg

And I want to cut it down to something like this (Delete -274x183) :

http://my-site.com/name.jpg

How can I do this with javascript or jQuery?

Thanks everyone

  • Possible duplicate of [javascript substring](http://stackoverflow.com/questions/1989009/javascript-substring) – freedomn-m Nov 02 '16 at 16:38
  • Broad bcuz the URL needs to be parsed. –  Nov 02 '16 at 16:39
  • 1
    @freedomn-m just String#substring won't do that safely. –  Nov 02 '16 at 16:41
  • 1
    @Someone the question is too vague, it's possible that they just want to remove those specific characters, or they may need a more generic features - there's no way to know given they've used the term "special characters" and there aren't any "special" characters in the example and they've requested jquery for basic string manipulation. – freedomn-m Nov 02 '16 at 17:16

1 Answers1

0

You don't need jQuery to do that. Use regular expressions instead:

const url = 'http://my-site.com/name-274x183.jpg'
const result = url.replace(/\/([^/]+)-\d+x\d+\.(\w+)$/, '/$1.$2')

console.log(result)
qzb
  • 8,163
  • 3
  • 21
  • 27