最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Replace number in a string using regex or something else - Stack Overflow

programmeradmin1浏览0评论

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?ticket=123432"> using javascript.

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.

Share Improve this question asked Apr 20, 2010 at 18:42 KrishnaKrishna 1,9665 gold badges22 silver badges30 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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>.

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>
发布评论

评论列表(0)

  1. 暂无评论