0

I am trying to replace a string text9 text text to 9 only.

The code below is working but returning literally {1}

$("body").html($("body").html().replace(/text([0-9]+)[ \t]text text/g,'{1}'));

Instead of returning literally {1}, I want it to return the [0-9]+ from the search.

How would you do that with Regex?

1 Answer 1

1

Replace your code

yourstring.replace(/text([0-9]+)[ \t]text text/g, '{1}');

with this:

yourstring.replace(/text([0-9]+)[ \t]text text/g, '$1');

and your code will be like:

$('body').html(function (_, html) {
    return html.replace(/text([0-9]+)[ \t]text text/g, '$1');
});

FIDDLE DEMO

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.