标签:http io ar os 使用 java sp on cti
文章转自 http://www.w3school.com.cn/jsref/jsref_replace.asp
字符 | 替换文本 |
---|---|
$1、$2、...、$99 | 与 regexp 中的第 1 到第 99 个子表达式相匹配的文本。 |
$& | 与 regexp 相匹配的子串。 |
$` | 位于匹配子串左侧的文本。 |
$‘ | 位于匹配子串右侧的文本。 |
$$ | 直接量符号。 |
在本例中,我们将使用 "W3School" 替换字符串中的 "Microsoft":
<script type="text/javascript">
var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/, "W3School")
)
</script>
输出:
Visit W3School!
在本例中,我们将执行一次全局替换,每当 "Microsoft" 被找到,它就被替换为 "W3School":
<script type="text/javascript">
var str="Welcome to Microsoft! "
str=str + "We are proud to announce that Microsoft has "
str=str + "one of the largest Web Developers sites in the world."
document.write(str.replace(/Microsoft/g, "W3School")
)
</script>
输出:
Welcome to W3School! We are proud to announce that W3School has one of the largest Web Developers sites in the world.
您可以使用本例提供的代码来确保匹配字符串大写字符的正确:
text = "javascript Tutorial"; text.replace(/javascript/i, "JavaScript");
在本例中,我们将把 "Doe, John" 转换为 "John Doe" 的形式:
name = "Doe, John"; name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1");
在本例中,我们将把所有的花引号替换为直引号:
name = ‘"a", "b"‘; name.replace(/"([^"]*)"/g, "‘$1‘");
在本例中,我们将把字符串中所有单词的首字母都转换为大写:
name = ‘aaa bbb ccc‘; uw=name.replace(/\b\w+\b/g, function(word){ return word.substring(0,1).toUpperCase()+word.substring(1);} );
标签:http io ar os 使用 java sp on cti
原文地址:http://www.cnblogs.com/yun13555/p/4089382.html