Panelen



Forumnavigering
Senast inloggade
RSS
Sponsorer

Forum > ASP.NET 1.x och 2.x > Språk - C#

Ersätta sträng(RegExp)

hejsan, jag undrar hur jag ersätter <?php med en annan sträng?
Jag har den här koden:

string BBCode(string strTextToReplace)
    {
        Regex regExp;
        regExp = new Regex("\\[url\\]([^\\]]+)\\[\\/url\\]");
        strTextToReplace = regExp.Replace(strTextToReplace, "<a href=\"$1\">$1</a>");
        regExp = new Regex("\\[url=([^\\]]+)\\]([^\\]]+)\\[\\/url\\]");
        strTextToReplace = regExp.Replace(strTextToReplace, "<a href=\"$1\">$2</a>");
        regExp = new Regex("\\[img\\]([^\\]]+)\\[\\/img\\]");
        strTextToReplace = regExp.Replace(strTextToReplace, "<img src=\"$1\" />");
        regExp = new Regex("\\[b\\](.+?)\\[\\/b\\]");
        strTextToReplace = regExp.Replace(strTextToReplace, "<b>$1</b>");
        regExp = new Regex("\\[i\\](.+?)\\[\\/i\\]");
        strTextToReplace = regExp.Replace(strTextToReplace, "<i>$1</i>");
        regExp = new Regex("\\[u\\](.+?)\\[\\/u\\]");
        strTextToReplace = regExp.Replace(strTextToReplace, "<u>$1</u>");
        regExp = new Regex("\\[size=([^\\]]+)\\]([^\\]]+)\\[\\/size\\]");
        strTextToReplace = regExp.Replace(strTextToReplace, "<span style=\"font-size: $1px\">$2</span>");
        regExp = new Regex("\\[color=([^\\]]+)\\]([^\\]]+)\\[\\/color\\]");
        strTextToReplace = regExp.Replace(strTextToReplace, "<span style=\"color: $1\">$2</span>");
        return strTextToReplace;
    }



Redigerad av Petters_Web
Anledning:

Its not about how hard you can hit, its how hard you can get hit, and still go on.
ID: #14502 | Skapades: 2009-02-15 16:41:42 | Tråden har 1 svar och har lästs 354 gånger.

voigtan
voigtan

Gradering

#14504

Prova:

        strTextToReplace = strTextToReplace.Replace("<?", "&lt;?");
        strTextToReplace = strTextToReplace.Replace("?>", "?&gt;");
placera den överst i din funktion, eller något liknande.

Hur man ställer en fråga: http://support.microsoft.com/kb/q555375
ID: #14504 | Skapades: 2009-02-15 17:00:28
© Copyright 2007-2009 Shadi Domat | Version 3.0 | Sidkarta | Policy