Javascript Regular Expressions in RegEx -
here's problem. want replace "document 1", "document 12" strings in text hypertext link :
<a onclick="gotodocument(document7);">document 7</a>
i tried code :
var reg=new regexp("((document )[a-za-z0-9/.]+)+\s(\w+)","gi"); var chaine= *thetext*; var socle_contenu = chaine.replace(reg, "<a onclick=\"alleraudocument('$1');\"'>" + '$1' + "</a>");
the result :
<a onclick="gotodocument(document 7);">document 7</a>
as can see, problem space in onclick function.
i tried delete space want keep space human reader. couldn't make it.
i tried "document " , number in $1 , $2, in order contacatenate 2 variables need. tried :
var reg=new regexp("((document )\s(\w+))+", "gi");
but doesn't work ! can tell me how setup regular expression ?
thanks !
you use callback function replace. allows further manipulation of matches:
var s = 'document 12 more text document 7 end'; var re = new regexp(/(document\s?\d+)/gi); s = s.replace(re, function($1){ return "<a onclick=\"alleraudocument('" + $1.replace(' ', '') + "');\">" + $1 + "</a>"; });
see demo here
Comments
Post a Comment