HTML

2016-04-09 21 views
0

içerebilen dizgiye uygulanacak bir Cümle Davası Uygula "Cümle vakasına" dönüştürmem gereken bir dizi dizim var, ancak bunu daha da karmaşık hale getiren dizenin, html bağlantı etiketlerine sahip olması gerektiğidir.HTML

<a href="/foo">foo</a> is a word. this is another word, and <a href="/bar">bar</a> is another. 

Ve şöyle Ben çıkışı ile cümle durumda uygulamak istiyorum:

biri böyle html içerebilir

<a href="/foo">Foo</a> is a word. This is another word, and <a href="/bar">bar</a> is another. 

Ben js veya vbscript kullanan herhangi çözümü kullanabilirsiniz.

+0

Ve ne denediniz? Bu kodu sorunuza gönderin. – Andy

+0

Bununla birlikte, bu soruna nasıl saldırılacağından emin değilim, bu yüzden mantıkla ilgili yardıma ihtiyacım var. Ben regex kullanarak bir bıçak alacağım, ama nasıl yaklaşacağımı bilmiyorum. – GWR

+0

Neden oylama? Bunun için mantıkla meşru bir şekilde yardım istedim. Sorunun uygun olmadığını ve aşağı oylamayı garanti ettiğini anlamama yardım et. – GWR

cevap

1

durumda birisi olarak. bakıyor, burada bir Rion Williams l limanı vbScript işlevine ogic. Kendi sınıf kütüphanemden birkaç fonksiyon kullandım, bu yüzden referans için sadece ihtiyaç duyulan parçaları da dahil ediyorum.

Rion'un dediği gibi, bu sadece bir başlangıç ​​ve çok fazla ince ayar gerektiriyor.

Function toSentenceCase(byVal x) 
    Dim i, r, s, bCapitalize, bInHtml 

    bCapitalize = True 
    bInHtml = False 

    Set r = New regularExpression 
    Set s = New adoStream 

    For i = 1 To Len(x) 
     sChar = Mid(x, i, 1) 
     Do 
      'If this is any non tag, punctuation or letter or we are in HTML and haven't encountered a closing tag 
      If r.test("[^a-zA-Z\?\.\>\\<\!]", sChar) Or (bInHtml And sChar <> ">") Then 
       s sChar 
       Exit Do 
      End If 

      'if we should capitalize, check if we can, and if yes, then capitalize 
      If bCapitalize And r.test("[a-zA-Z]", sChar) Then 
       s uCase(sChar) 
       bCapitalize = False 
       Exit Do 
      Else 
       s sChar 

       'if this character is '<', then we are in HTML, so ignore these 
       If sChar = "<" Then 
        bInHtml = True 
        Exit Do 
       End If 

       'if the character is a closing tag '>', then start paying attention again 
       If sChar = ">" Then 
        bInHtml = False 
        Exit Do 
       End If 

       'determine if we hit punctuation to start a new sentence 
       If r.test("[\?\!\.]", sChar) Then 
        bCapitalize = True 
        Exit Do 
       End If 

      End If 

     Loop While False 
    Next 

    toSentenceCase = s.Value 
End Function 

Class adoStream 
    'string builder class. adodb streams are way faster than appending to/editing content of string variables 
    Private stream 

    Private Sub Class_Initialize()     
     Set stream = CreateObject("ADODB.Stream") 
     stream.Type = 2 '2 = text stream 
     stream.Open 
    End Sub 

    Private Sub Class_Terminate() 
     stream.Close 
     Set stream = Nothing 
    End Sub 

    Public Default Sub Add(byVal addString) 'add string to existing stream 
     stream.WriteText addString 
    End Sub 

    Public Sub Update(byVal addString) 'update existing stream and set it to a new value. clear existing stream and set it = new value 
     Clear 
     stream.WriteText addString 
    End Sub 

    Public Property Get Value 'returns full stream 
     stream.Position = 0 
     Value = stream.ReadText() 
    End Property 

    Public Function Clear() 'resets stream 
     stream.Position = 0 
     Call stream.SetEOS() 
    End Function   
End Class 


Class regularExpression 
    'class containing a set of vbscript regex routines 
    Private oRegex 
    Private Sub Class_Initialize()     
     Set oRegex = New RegExp 
     oRegex.Global = True 'e.g. findall 
     oRegex.IgnoreCase = True 
    End Sub 

    Private Sub Class_Terminate() 
     Set oRegex = Nothing 
    End Sub 

    'test 
    Public Function test(byVal sPattern, byVal sTestString) 'return t/f 
     If isNull(sTestString) Then 
      test = False 
      Exit Function 
     End If 
     oRegex.Pattern = sPattern 
     test = oRegex.test(sTestString) 
    End Function 
End Class 
2

sana karşılaştığı dayalı dize ve bayraklar koşullar aracılığıyla basitçe yineler (yani buna bir HTML etiketine olduğunu belirtmek için bir inHtml bayrağı ayarlar oldukça naif bir yaklaşım oluşturmak ve başka shouldCapitalize bayrağı ayarlar olabilir varsayalım belirlemek, bir cümlenin başında ise: doğrusu aceleyle birlikte attı

function titleCaseHtmlSentence(s){ 
    // A temporary string to hold your results 
    var result = ''; 
    // Iterate through the sentence and check each character to determine if 
    // it is the start of a sentence, ignore this 
    var shouldCapitalize = true; 
    var inHtml = false; 
    for(var i = 0; i < s.length; i++){ 
     // If this is any non tag, punctuation or letter or we are in HTML 
     // and haven't encountered a closing tag 
     if(/[^a-zA-Z\?\.\>\\<\!]/.test(s[i]) || (inHtml && s[i] != '>')){ 
      result += s[i]; 
      continue; 
     } 
     // If we should capitalize, check if we can 
     if(shouldCapitalize && /[a-zA-Z]/.test(s[i])){ 
      // Capitalize this character 
      result += s[i].toUpperCase(); 
      shouldCapitalize = false; 
      continue; 
     } 
     else{ 
      result += s[i]; 
      // If this character is '<', then we are in HTML, so ignore these 
      if(s[i] == '<'){ 
       inHtml = true; 
       continue; 
      } 
      // If the character is a closing tag '>', then start paying attention again 
      if(s[i] == '>'){ 
       inHtml = false; 
       continue; 
      } 

      // Determine if we hit punctuation to start a new sentence 
      if(/[\?\!\.]/.test(s[i])){ 
       shouldCapitalize = true; 
       continue; 
      } 
     } 
    } 
    return result; 
} 

, bu yüzden herhangi bir anlamda optimal olmaktan uzak olduğundan eminim ama seen in this example olarak çalışması gerekir