_ _    _ _____  ___   __                       
 __      _(_) | _(_)___ / ( _ ) / /_   ___ ___  _ __ ___  
 \ \ /\ / / | |/ / | |_ \ / _ \| '_ \ / __/ _ \| '_ ` _ \ 
  \ V  V /| |   <| |___) | (_) | (_) | (_| (_) | | | | | |
   \_/\_/ |_|_|\_\_|____/ \___/ \___(_)___\___/|_| |_| |_|

Utilisateur:Zelda/regexp.js

Dans cet article, nous explorerons le monde fascinant de Utilisateur:Zelda/regexp.js et tout ce qu'il a à offrir. De ses origines à son actualité aujourd’hui, nous nous plongerons dans un large éventail d’informations qui nous permettront de mieux comprendre ce sujet. Nous analyserons ses différentes facettes, explorerons ses impacts sur la société et réfléchirons à son avenir. Rejoignez-nous dans cette incroyable tournée et découvrez tout ce que Utilisateur:Zelda/regexp.js a à nous apprendre.
//<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 =
		{"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;
		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",
		"http://upload.wikimedia.org/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>