Vbscript-replace-function
提供:Dev Guides
VBScript置換関数
交換する
置換関数は、指定された回数、文字列の指定された部分を特定の文字列に置き換えます。
構文
Replace(string,find,replacewith[,start[,count[,compare]]])
文字列、必須パラメーター。 置換のために検索される入力文字列。
find、必須パラメーター。 置換される文字列の一部。
必須パラメーターに置き換えます。 検索パラメーターに対して置換される置換文字列。
start、オプションのパラメーター。 文字列を検索して置換する開始位置を指定します。 デフォルト値は1です。
count、オプションのパラメーター。 置換を実行する必要がある回数を指定します。
compare、オプションのパラメーター。 使用する比較方法を指定します。 デフォルト値は0です。
- 0 = vbBinaryCompare-バイナリ比較を実行します
- 1 = vbTextCompare-テキスト比較を実行します
例
<!DOCTYPE html>
<html>
<body>
<script language = "vbscript" type = "text/vbscript">
var = "This is VBScript Programming"
'VBScript to be replaced by MS VBScript
document.write("Line 1: " & Replace(var,"VBScript","MS VBScript") & "<br/>")
'VB to be replaced by vb
document.write("Line 2: " & Replace(var,"VB","vb") & "<br/>")
''is' replaced by ##
document.write("Line 3: " & Replace(var,"is","##") & "<br/>")
''is' replaced by ## ignores the characters before the first occurence
document.write("Line 4: " & Replace(var,"is","##",5) & "<br/>")
''s' is replaced by ## for the next 2 occurences.
document.write("Line 5: " & Replace(var,"s","##",1,2) & "<br/>")
''r' is replaced by ## for all occurences textual comparison.
document.write("Line 6: " & Replace(var,"r","##",1,-1,1) & "<br/>")
''t' is replaced by ## for all occurences Binary comparison
document.write("Line 7: " & Replace(var,"t","##",1,-1,0) & "<br/>")
</script>
</body>
</html>
lとして保存してInternet Explorerで実行すると、上記のスクリプトは次の結果を生成します-
Line 1: This is MS VBScript Programming
Line 2: This is vbScript Programming
Line 3: Th## ## VBScript Programming
Line 4: ## VBScript Programming
Line 5: Thi## i## VBScript Programming
Line 6: This is VBSc##ipt P##og##amming
Line 7: This is VBScrip## Programming