Javascript/Jquery。使用带通配符的正则表达式将纯文本替换为html
我想将纯文本(例如)[next 1272]替换为Javascript/Jquery。使用带通配符的正则表达式将纯文本替换为html,javascript,regex,wildcard,Javascript,Regex,Wildcard,我想将纯文本(例如)[next 1272]替换为 <a href='page.asp?id=1272'> <img src='next.png' alt='Next Page' title='Next Page' /> </a> 好吧,我想了想,下面的作品,以防对任何人都有帮助。 但不是很优雅 regex = new RegExp(/\[next (.*?)\]/gi); mtch=ThisBody.match(regex) newBody=ThisBody
<a href='page.asp?id=1272'>
<img src='next.png' alt='Next Page' title='Next Page' />
</a>
好吧,我想了想,下面的作品,以防对任何人都有帮助。 但不是很优雅
regex = new RegExp(/\[next (.*?)\]/gi);
mtch=ThisBody.match(regex)
newBody=ThisBody
if (mtch!=null)
{
if (mtch.length>0)
{
for (var i=0; i<mtch.length; i++)
{
tmp=mtch[i] // [next 1272]
tmp=tmp.replace("]","") // [next 1272
tmp=tmp.substring(6) // 1272
t="<a href='page.asp?id=" + tmp + "'>"
t+="<img src='Next.png' alt='Next Page' title='Next Page' />"
t+="</a>"
newBody=newBody.replace(mtch[i],t)
}
}
}
ThisBody=newBody
regex=newregexp(/\[next(.*?\]/gi);
mtch=ThisBody.match(正则表达式)
新身体=这个身体
如果(mtch!=null)
{
如果(平均长度>0)
{
对于(var i=0;i
regex = new RegExp(/\[next (.*?)\]/gi);
mtch=ThisBody.match(regex)
newBody=ThisBody
if (mtch!=null)
{
if (mtch.length>0)
{
for (var i=0; i<mtch.length; i++)
{
tmp=mtch[i] // [next 1272]
tmp=tmp.replace("]","") // [next 1272
tmp=tmp.substring(6) // 1272
t="<a href='page.asp?id=" + tmp + "'>"
t+="<img src='Next.png' alt='Next Page' title='Next Page' />"
t+="</a>"
newBody=newBody.replace(mtch[i],t)
}
}
}
ThisBody=newBody