Prototype - sub() 方法
此方法返回一个字符串,其中 pattern 的前 count 个出现次数被替换为常规字符串、函数的返回值或模板字符串。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>单击按钮查看结果。</p>
<br />
<br />
<input type = "button" value = "Result" onclick = "showResult();"/>
</body>
</html>
输出
prototype_string_processing.html

