javascript中replace的正则表达式语法
replace
方法
以下是javascript
中的例子
下面的示例演示了 replace
方法将第一次出现的单词 "The"
替换为单词 "A"
的用法。
function ReplaceDemo(){
var r, re; //
声明变量。
var ss = "The man hit the ball
with the bat.\n";
ss += "while the fielder caught
the ball with the glove.";
re = /The/g; //
创建正则表达式模式。
r = ss.replace(re,
"A"); //
用 "A"
替换 "The"
。
return(r); //
返回替换后的字符串。
}
另外, replace
方法也可以替换模式中的子表达式。 下面的范例演示了交换字符串中的每一对单词:
function ReplaceDemo(){
var r, re; //
声明变量。
var ss = "The rain in Spain falls
mainly in the plain.";
re = /(\S+)(\s+)(\S+)/g; //
创建正则表达式模式。
r = ss.replace(re,
"$3$2$1"); //
交换每一对单词。
return(r); //
返回结果字符串。
}
下面的示例(在 JScript 5.5
及更新版本中执行)执行的是从华氏到摄氏的转换,它演示了使用函数作为 replaceText
。要想知道该函数是如何工作的,传递一个包含数值的字符串,数值后要紧跟 "F"
(例如 "Water boils at
212"
)。
function f2c(s) {
var test = /(\d+(\.\d*)?)F\b/g; //
初始化模式。
return(s.replace
&nb
相关文档:
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"Cambria Mat ......
在web开发过程中,经常会出现因为客户端的某些软件版本问题,而导致种种问题。这来天就因为Media Player版本的问题,引发了一个不大不小的问题。在调用Media Player播放视频的时候,Media Player9竟然播放不了 ... ...
不多说了,还是贴代码吧!
<html xmlns="h ......
1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键
<table border oncontextmenu=return(false)> <td>no </table> 可用于Table
2. <body onselectstart="return false"> 取消选取、防止复制
3. onpaste="return false" 不准粘贴
4. oncopy="return false;" oncut=" ......
在今天网络开发方面,JavaScript起了很关键的作用;像jQuery, MooTools, Prototype等等JavaScript框架以及其它JavaScript类库让我们的生活轻松了不少。但是随着Rich Internet Applications(RIA)的面世及迅速应用,书写更强大,更坚实可靠的JavaScript的需要日益迫切。
不管你是JavaSc ......
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>drag类</title ......