If text string contains certain words, wrap those words in a span tag

爷,独闯天下 提交于 2019-12-11 08:06:45

问题


Looking for a little help here. I have a text string like:

> ...And The World Laughs With You (feat Thom Yorke)

With vbscript, if the text string has "(feat " in it, I want to wrap the whole bracketed text in a span tag.
So, the example above would look like:

> ...And The World Laughs With You <span>(feat Thom Yorke)</span>

Hope this makes sense and thank you in advance for the help!

Cheers,
Drew


回答1:


Try with this logic.

     dim str 
   str = "And The World Laughs With You (feat Thom Yorke)"

   If  INSTR(str,"(feat ") > -1 Then
        str = REPLACE(str,"(feat ","<span>(feat ") 
        str = REPLACE(str,")",")</span>") 
   End If

   Response.Write("Result:- "  + str)  



回答2:


See the examples.

Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout

Sub ReplaceCmd
    'Remove ^ from quoting command line. Quote, ampersand and brackets
    Pttn = Replace(Arg(2), "^(", "(")
    Pttn = Replace(Pttn, "^)", ")")
    Pttn = Replace(Pttn, "^&", "&")
    Pttn = Replace(Pttn, "^""", """")
    Set regEx1 = New RegExp
    If Instr(LCase(Arg(1)), "i") > 0 then
        regEx1.IgnoreCase = True
    Else
        regEx1.IgnoreCase = False
    End If 
    regEx1.Global = False
    regEx1.Pattern = Pttn 
    Do Until Inp.AtEndOfStream
        Line=Inp.readline
        Line = RegEx1.Replace(Line, Arg(3)) 
        outp.writeline Line
    Loop
End Sub

To use cscript scriptname <infile >outfile

Replace

filter replace {i|n} expression replace
filter repl {i|n} expression replace

Finds and replaces text using regular expressions.

Also used to extract substrings from a file.

Ampersands and brackets in expression must be escaped with the caret. Do not escape carets. Use hexidecimal code \x22 for quotes.

SearchOptions

i - ignore case n - none Expression

Regular Expression Reference

Replace

The text to replace. Use $1, $2, $..., $n to specify sub matches in the replace string

Example

filter replace i "=" "No equal sign" < "%systemroot%\win.ini"

This searches for text within square brackets and replaces the line with cat followed by the text within brackets

Filter replace i "^\[^(.*^)\]" "cat$1" < %windir%\win.ini

This searches for any text and prints from the 11th character to the end of the line.

Filter replace i "^.{10}^(.*^)$" "$1" < %windir%\win.ini

This searches a CSV file and prints the second and fourth field

Filter replace i "^.+,^(.+^),.+,^(.+^)$" "$1,$2" < csv.txt


来源:https://stackoverflow.com/questions/29483379/if-text-string-contains-certain-words-wrap-those-words-in-a-span-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!