-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

4
  • Possible duplicate of javascript substring Commented Nov 2, 2016 at 16:38
  • Broad bcuz the URL needs to be parsed. Commented Nov 2, 2016 at 16:39
  • 1
    @freedomn-m just String#substring won't do that safely. Commented Nov 2, 2016 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. Commented Nov 2, 2016 at 17:16

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.