jQuery string replace with regex not working

String:

remove_old('pad3.jpeg','3')

jQuery:

var new_value = onchange_value.replace("remove_old(", "");
new_value = new_value.replace(")", "");

Expected Output:

'pad3.jpeg','3'

I want to remove remove_old(). Anyone can please tell me How can I remove this by using regex. Sorry for my weak English.

Answers 1

  • You can use the regex:

    /\((.+)\)/
    

    Broken down:

    • \( match a (
    • ( start matching group
    • . any character
    • + one or more of previous (any character, so at least one character)
    • ) close matching group
    • \) match )

    Then

    src.match(regex)[1] takes the first match-group (inside the ()).

    var src = "remove_old('pad3.jpeg','3')";
    var expected = "'pad3.jpeg','3'";
    var updated = src.match(/\((.+)\)/)[1];
    console.log(expected==updated)
    console.log(updated)


Related Articles