Utilisateur:Delhovlyn/regex.js
Un article de Wikipédia, l'encyclopédie libre.
Note : Après avoir publié la page, vous devrez forcer son rechargement pour voir les changements : Mozilla / Konqueror / Firefox : Shift-Ctrl-R, Internet Explorer / Opera : Ctrl-F5, Safari : Cmd-R.
<code lang="javascript"> function regexpReplace(regexp, replace) { var txtarea = document.getElementById("Fiche"); // IE if (document.selection && !is_gecko) { var range = document.selection.createRange(); var theSelection = range.text; if (!theSelection) theSelection = txtarea.value; txtarea.focus(); if (theSelection.charAt(theSelection.length - 1) == " ") { // exclude ending space char, if any theSelection = theSelection.substring(0, theSelection.length - 1); range.text = theSelection.replace(regexp, replace) + " "; } else { range.text = theSelection.replace(regexp, replace); } // Mozilla } else if(txtarea.selectionStart || txtarea.selectionStart == '0') { var replaced = false; var startPos = txtarea.selectionStart; var endPos = txtarea.selectionEnd; if (endPos-startPos <= 0) { startPos = 0; endPos = txtarea.value.length; } var scrollTop = txtarea.scrollTop; var theSelection = (txtarea.value).substring(startPos, endPos); var replacedSelection = theSelection.replace(regexp, replace); txtarea.value = txtarea.value.substring(0, startPos) + replacedSelection + txtarea.value.substring(endPos, txtarea.value.length); txtarea.focus(); //set new selection var cPos = startPos+(replacedSelection.length); txtarea.selectionStart = startPos; txtarea.selectionEnd = cPos; //txtarea.scrollTop = scrollTop; } } </code>