原型 -子()方法
此方法返回一個字串,其中 pattern 的前 count 個出現由常規字串、函式的返回值或 Template 字串替換。Pattern 可以是字串或正則表示式。
語法
string.sub(pattern, replacement[, count = 1]);
返回值
返回一個字串。
例子
<html>
<head>
<title>Prototype examples</title>
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
<script>
function showResult() {
var fruits = 'apple pear orange';
alert("fruits.sub(' ', ', '):"+ fruits.sub(' ', ', '));
alert("fruits.sub(' ', ', ', 1): " + fruits.sub(' ', ', ', 1));
alert("fruits.sub(' ', ', ', 2): " + fruits.sub(' ', ', ', 2));
var str = fruits.sub(/\w+/,
function(match) { return match[0].capitalize() + ','}, 2);
alert(str);
}
</script>
</head>
<body>
<p>Click the button to see the result.</p>
<br />
<br />
<input type = "button" value = "Result" onclick = "showResult();"/>
</body>
</html>
輸出
prototype_string_processing.htm
廣告