Utilisateur:Stef48/regexp.js
Apparence
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;
Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.//<source lang="javascript">
// Tableau de boutons
var regexpButtons = [];
/**
* Ajout d'un bouton а la toolbar d'edition
* inspiree de insertButton()
* @param title : titre de la fonction
* @param image : source de l'image а afficher
* @param href: lien a executer lors du click sur le bouton
* @param accesskey : touche d'acces rapide (optionnel)
*/
function regexpAddButton(title, image, href, accesskey) {
regexpButtons[regexpButtons.length] =
{"title": title,
"image": image,
"href": href,
"accesskey": accesskey};
}
/**
* Ajoute les boutons à la toolbar
*/
function regexpAddButtons() {
var toolbar = document.getElementById("toolbar");
if (!toolbar) return;
for (var i in regexpButtons) {
var button = regexpButtons[i];
var img = document.createElement("img");
img.width = 23;
img.height = 22;
img.src = button.image;
img.border = 0;
img.style.cursor = "pointer";
img.alt = button.title;
var ref = document.createElement("a");
if (!button.flags) button.flags = "";
ref.setAttribute("href", button.href);
ref.setAttribute("title", button.title);
if (button.accesskey) {
ref.setAttribute("accesskey", button.accesskey);
}
ref.appendChild(img);
toolbar.appendChild(ref);
}
}
/**
* fonction la recherche et le remplacement de texte
* inspiree de insertTags(), elle meme inspiree de phpBB,
* en utilisant des expression regulieres
*/
function regexpReplace(regexp, replace) {
var txtarea = document.getElementById("wpTextbox1");
// 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;
}
}
/**
* Ajout dans la toolbar d'un bouton chercher/remplacer
*/
function addReplaceButton() {
regexpAddButton("Remplacer",
"/media/wikipedia/commons/5/59/Button_replace.png",
"javascript:regexpReplace(" +
"new RegExp(window.prompt('Chercher (expression régulière)')," +
"window.prompt('Flags (optionel, combinaison de m, i, g)'))," +
"window.prompt('Remplacer par'));",
null);
}
$(addReplaceButton);
$(regexpAddButtons);
//</source>