此方法返回一个字符串,其中 pattern 的第一次出现次数被替换为常规字符串、函数的返回值或模板字符串。模式可以是字符串或正则表达式。
语法
string.sub(pattern, replacement[, count = 1]);
返回值
返回一个字符串。
例子
HTML
x
26
26
1
<html>
2
<head>
3
<title>Prototype examples</title>
4
<script type = "text/javascript" src = "/javascript/prototype.js"></script>
5
6
<script>
7
function showResult() {
8
var fruits = 'apple pear orange';
9
alert("fruits.sub(' ', ', '):"+ fruits.sub(' ', ', '));
10
alert("fruits.sub(' ', ', ', 1): " + fruits.sub(' ', ', ', 1));
11
alert("fruits.sub(' ', ', ', 2): " + fruits.sub(' ', ', ', 2));
12
var str = fruits.sub(/\w+/,
13
function(match) { return match[0].capitalize() + ','}, 2);
14
alert(str);
15
16
}
17
</script>
18
</head>
19
20
<body>
21
<p>Click the button to see the result.</p>
22
<br />
23
<br />
24
<input type = "button" value = "Result" onclick = "showResult();"/>
25
</body>
26
</html>
阅读剩余 78%
作者:terry,如若转载,请注明出处:https://www.web176.com/prototype_api/8831.html