2015-04-07 98 views
1

在此處尋找幫助。我有一個文本字符串,如:如果文本字符串包含某些單詞,請將這些單詞包裹在span標記中

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

隨着vbscript,如果文本串中有"(feat ",我想包裝在span標記整個括號中的文字。
所以,上面的例子看起來像:

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

希望這是有道理的,謝謝你提前的幫助!

乾杯,
德魯

+0

你的問題是什麼? –

回答

2

與此邏輯嘗試。

 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) 
+0

這不是VBScript。 –

+0

@ Ekkehard.Horner - 我已將代碼更改爲vbscript。 –

0

見例子。

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 

要使用cscript scriptname <infile >outfile

更換

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

發現和使用正則表達式替換文本。

也用於從文件中提取子字符串。

表達式中的&符號和括號內必須使用脫字符。不要逃脫插入。使用十六進制代碼\ x22作爲引號。

SearchOptions

我 - 忽略大小寫 N - 無 表達

正則表達式參考

更換

替換的文本。使用$ 1,$ 2,$ ...,$ N來替換字符串中指定的子場比賽

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

這種搜索方括號內的文字和替換貓行後跟內的文本括號

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

這將搜索任何文本並從第11個字符打印到行尾。

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

此搜索的CSV文件,並打印所述第二和第四場

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

@Jinesh Jain你很好,你從我的複製你的答案。 – Serenity

相關問題