9

I am not so good with regex. I am struggling to find a solution for a small functionality.

I have a ajax response which returns a string like "Your ticket has been successfully logged. Please follow the link to view details 123432."

All I have to do is replace that number 123432 with <a href="blablabla.com?ticket=123432"> using javascript.

2 Answers 2

17

Try this:

fixedString = yourString.replace(/(\d+)/g, 
    "<a href='blablabla.com?ticket=$1\'>$1</a>");

This will give you a new string that looks like this:

Your ticket has been successfully logged. Please follow the link to view details <a href='blablabla.com?ticket=123432'>123432</a>.

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

3 Comments

Notice that the replacement text is not constant.
Shouldn't that be fixedString = yourString.replace(/(\d+)/g, "<a href='blablabla.com?ticket=$1\'>$1</a>");
@Chad - Yes it should - nice call :)
1
var str = "Your ticket has been successfully logged. Please follow the link to view details 123432.";
str = str.replace(/\s+(\d+)\.$/g, '<a href="blablabla.com?ticket=$1">$&</a>');

this code will output

<a href="blablabla.com?ticket=123432">Your ticket has been successfully logged. Please follow the link to view details 123432.</a>

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.