Visualizzazione dei risultati da 1 a 10 su 10
  1. #1
    Utente di HTML.it L'avatar di gasmor
    Registrato dal
    Apr 2004
    Messaggi
    1,768

    eseguire una funzione dentro replace

    ciao

    come faccio ad eseguire una funzione dentro un replace?
    Ho provato così ma non ne vuole sapere

    codice:
    	function sosti(str){
    		str=str.replace(/</gmi, "&lt;");
    		str=str.replace(/>/gmi, "&gt;");		
    		return str;
    	}
    	
    	regeXp = new RegExp('\\[YT[^\\]]*\\](.*?)\\[/YT\\]', 'gmi');
    	out = out.replace(regeXp,'[YT]'+sosti('$1')+'[/YT]');
    Hai provato a creare un sito con Jaaxo Cms PHP5? new!

  2. #2
    Utente di HTML.it L'avatar di gasmor
    Registrato dal
    Apr 2004
    Messaggi
    1,768
    premetto che ho anche fatto una cosa del genere:

    codice:
    	regeXp = new RegExp('\\[YT[^\\]]*\\](.*?)\\[/YT\\]', 'gmi');
    	var videoText = regeXp.exec(testo_inFrame);
    	if(videoText[1]!=null){
    		videoText[1]=videoText[1].replace(/</gmi, "&lt;");
    		videoText[1]=videoText[1].replace(/>/gmi, "&gt;");
    		out = out.replace(regeXp,'[YT]'+videoText[1]+'[/YT]')
    	}
    ma non va bene perchè in caso di più occorrenze me le modifiche tutte con l'ultima (in quanto exec restituisce sempre l'ultima occorrenza)
    Hai provato a creare un sito con Jaaxo Cms PHP5? new!

  3. #3
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    serve una function anonima

    adattati questo esempio
    codice:
    	// rende minuscoli solo tag e attributi lasciando inalterati i valori
    	output=output.replace(/<[^>]+>/g, function(t) { 
    		var p=t.split(" ");
    			for (var i=0;i<p.length;i++) {
    				p[i] = p[i].replace(/<\/?.+/, function(x) { return x.toLowerCase() }); // nome tag
    				p[i] = p[i].replace(/[^=]+=/, function(x) { return x.toLowerCase() }); // nome attributo
    			}
    		return p.join(" ");
    	})

  4. #4
    Utente di HTML.it L'avatar di gasmor
    Registrato dal
    Apr 2004
    Messaggi
    1,768
    Originariamente inviato da Xinod
    serve una function anonima

    adattati questo esempio
    codice:
    	// rende minuscoli solo tag e attributi lasciando inalterati i valori
    	output=output.replace(/<[^>]+>/g, function(t) { 
    		var p=t.split(" ");
    			for (var i=0;i<p.length;i++) {
    				p[i] = p[i].replace(/<\/?.+/, function(x) { return x.toLowerCase() }); // nome tag
    				p[i] = p[i].replace(/[^=]+=/, function(x) { return x.toLowerCase() }); // nome attributo
    			}
    		return p.join(" ");
    	})
    ho fatto così:
    out = out.replace(regeXp,'[YT]'+function(x){ return x.replace(/>/gmi, "&gt;");}+'[/YT]');

    ma come gli passo il parametro $1 ? :master:
    Hai provato a creare un sito con Jaaxo Cms PHP5? new!

  5. #5
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    lo passi come secondo argomento della funzione
    codice:
    out = out.replace(regeXp,function(x,y){return "[YT]"+y.replace(/>/gmi,"&gt;").replace(/</gmi, "&lt;")+"[/YT]";});

  6. #6
    Utente di HTML.it L'avatar di gasmor
    Registrato dal
    Apr 2004
    Messaggi
    1,768
    grazie,

    però va con firefox ma non funziona con IE 7

    e poi volevo chiederti una cosa: ma y assume automaticamente il valore di $1?
    Cioè ho utilizzato il tuo codice senza cambiare una virgola e non capisco come riesca a prendere $1 senza che l'abbia specificato
    Hai provato a creare un sito con Jaaxo Cms PHP5? new!

  7. #7
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    a me funziona perfettamente anche in IE7

  8. #8
    Utente di HTML.it L'avatar di gasmor
    Registrato dal
    Apr 2004
    Messaggi
    1,768
    Originariamente inviato da Xinod
    a me funziona perfettamente anche in IE7
    ok sono riuscito, si creava un problema con il designMode del campo.

    ma come fa y ad assumere automaticamente il valore di $1?
    Hai provato a creare un sito con Jaaxo Cms PHP5? new!

  9. #9
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    lambda expression

    http://developer.mozilla.org/en/docs...String:replace
    You get the matched substrings through the parameters of your function. The first parameter of your function holds the complete matched substring. If the first argument was a RegExp object, then the following n parameters can be used for parenthetical matches, remembered submatch strings, where n is the number of submatch strings in the regular expression. Finally, the last two parameters are the offset within the string where the match occurred and the string itself
    http://www.webreference.com/js/column26/lambda.html
    The first parameter of the function is the complete matched substring. Other parameters can be used for matched substrings, often called "parenthetical matches" (because they are delimited with parenthesis in the regular expression).
    e' piu' chiaro adesso?

  10. #10
    Utente di HTML.it L'avatar di gasmor
    Registrato dal
    Apr 2004
    Messaggi
    1,768
    si, grazie
    Hai provato a creare un sito con Jaaxo Cms PHP5? new!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.