2016-04-09 6 views
0

「文例」に変換する必要がある一連の文字列がありますが、文字列がHTMLのアンカータグを持つ可能性があります。HTMLを含む可能性のある文字列に文例を適用する

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

を、次のように私は出力と文ケースを適用したいと思います:

一つは、このようなHTMLが含まれている場合があり

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

私はjsまたはvbscriptを利用する任意のソリューションを使用することができます。

+0

何を試しましたか?あなたの質問にそのコードを投稿してください。 – Andy

+0

これは私が問題を攻撃する方法がわからないので、私はロジックに助けが必要だと思います。私は正規表現を使用してそれに刺すが、私はそれにアプローチする方法がわからない。 – GWR

+0

投票がなぜ失敗するのですか?私は合法的にこれについての論理で助けを求めた。その質問が適切ではないことを理解し、下位票を必要とすることを助けてください。 – GWR

答えて

1

場合誰かに。探している、ここにはリオンウィリアムズのポートがある機能はvbScriptになります。私は自分のクラスライブラリからいくつかの関数を使っていました。

リオンが言ったように、これはほんの始まりであり、多くの微調整が必​​要です。

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

私は、遭遇したことに基づいて文字列とフラグの条件を反復する単純な方法を構築することができます(つまり、inHtmlフラグを設定してHTMLタグ内にあることを示し、別のshouldCapitalizeフラグを決定は、文の先頭にいた場合:私はむしろ、急いでそれを一緒に投げた

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; 
} 

ので、私はそれがいかなる意味において最適なほど遠いだと確信していますが、それはseen in this exampleとして動作するはず

関連する問題