0

I have string like "key-original-original" and I want to replace it with "key-original-resized".

somebody help me with this by using regular expression and replace function in Javascript ?

3
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Also this is a duplicated question ;) Commented Jan 23, 2018 at 14:01
  • Hi. Can you post some code sample that you have tried ? Commented Jan 23, 2018 at 14:03
  • 3
    I'm voting to close this question as off-topic because the OP did not even google js replacce Commented Jan 23, 2018 at 14:05

2 Answers 2

2
<script>
      text = 'key-original-original';

    var t=0;   
    text = text.replace(/\-original/g, function (match) {
      t++;
      return (t === 2) ? "-resized" : match;
    });
    alert(text);
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

You may try this:

var str = 'key-original-original';
var newStr = str.replace(/key\-(.*)\-(.*)/i, 'key-$1-resized');

1 Comment

Here is a pretty good tutorial on working with strings. digitalocean.com/community/tutorials/… I know you asked for a regular expression but there are many combinations to do this. If you look through the quick walk-through I am sure you'll find something of your liking.

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.